Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Simplify developer experience, no need for curl -H just browse a url #123

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions assets/k8s/serviceAccount/cluster-role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ rules:
- ingresses
- services
- secrets
- configmaps
verbs:
- create
- delete
Expand Down
43 changes: 43 additions & 0 deletions src/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/nearform/initium-cli/src/services/project"
"github.com/nearform/initium-cli/src/utils/defaults"
"github.com/urfave/cli/v2"

knative "github.com/nearform/initium-cli/src/services/k8s"
)

const (
Expand Down Expand Up @@ -101,6 +103,32 @@ func (c icli) InitServiceAccountCMD(ctx *cli.Context) error {
return k8s.GetServiceAccount(c.Resources)
}

func (c icli) InitKnativeDomainCMD(cCtx *cli.Context) error {
config, err := knative.Config(
cCtx.String(endpointFlag),
cCtx.String(tokenFlag),
[]byte(cCtx.String(caCRTFlag)),
)

if err != nil {
return err
}

if cCtx.NArg() < 1 {
return fmt.Errorf("Knative domain argument is required!")
}

knativeDomain := cCtx.Args().Get(0)

err = knative.DomainUpd(knativeDomain, config)

if err != nil {
return err
}

return nil
}

func (c icli) InitCMD() *cli.Command {
configFlags := c.CommandFlags([]FlagsType{Shared})
configFlags = append(configFlags, &cli.BoolFlag{
Expand Down Expand Up @@ -133,6 +161,21 @@ func (c icli) InitCMD() *cli.Command {
Action: c.InitServiceAccountCMD,
Before: c.baseBeforeFunc,
},
{
Name: "knative-domain",
Usage: "updates knative service default domain",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make clear that a positional argument is required here?

PS. I've tried it with test.example.com and the config map now has both the domains, is this expected?

➜  initium-cli git:(chore/knative_domain) ✗ kubectl get cm config-domain -n knative-serving -o yaml
apiVersion: v1
data:
  example.com: ""
  test.example.com: ""

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I have added a condition to look for an argument and if not to error out with a msg.
  • The behaviour is what I mentioned yesterday when you suggested that maybe tilt or argocd reverts to example.com. Will investigate..

Flags: c.CommandFlags([]FlagsType{Kubernetes}),
Action: c.InitKnativeDomainCMD,
Before: func(ctx *cli.Context) error {
if err := c.loadFlagsFromConfig(ctx); err != nil {
return err
}

ignoredFlags := []string{namespaceFlag}

return c.checkRequiredFlags(ctx, ignoredFlags)
},
},
},
}
}
10 changes: 10 additions & 0 deletions src/cli/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,13 @@ func TestAppName(t *testing.T) {
t.Errorf("the error message should contain %s and %s", appNameFlag, repoNameFlag)
}
}

func TestKnativeDomain(t *testing.T) {
cli := GeticliForTesting(os.DirFS("../.."))

err := cli.Run([]string{"initium", "init", "knative-domain"})
if err == nil {
t.Errorf("CLI should ask for an argument (knative new domain) if not detected")
}

}
33 changes: 33 additions & 0 deletions src/services/k8s/knative.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,36 @@ func Clean(namespace string, config *rest.Config, project *project.Project) erro
log.Info("The Knative service was successfully deleted", "host", config.Host, "name", project.Name, "namespace", namespace)
return nil
}

func DomainUpd(kn_domain string, config *rest.Config) error {
// Default Knative values
configMapName := "config-domain"
namespace := "knative-serving"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not be hardcoding this imho, it limits the options in case someone has knative deployed in a different namespace.

Let me see if we can set the right default for the namespace flag

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checking if we can use namespace flag..


log.Info("Updating Knative default domain name...", "new domain", kn_domain, "configMap", configMapName, "namespace", namespace)
ctx := context.Background()

client, err := kubernetes.NewForConfig(config)
if err != nil {
return fmt.Errorf("Creating Kubernetes client %v", err)
}

configMap, err := client.CoreV1().ConfigMaps(namespace).Get(ctx, configMapName, metav1.GetOptions{})

if err != nil {
return fmt.Errorf("Getting ConfigMaps: %v", err)
}

configMap.Data = make(map[string]string)

configMap.Data[kn_domain] = ""

_, err = client.CoreV1().ConfigMaps(namespace).Update(ctx, configMap, metav1.UpdateOptions{})
if err != nil {
return err
}

log.Info("Knative default domain name was successfully updated!", "new domain", kn_domain, "configMap", configMapName, "namespace", namespace)

return nil
}
1 change: 0 additions & 1 deletion src/services/k8s/knative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,3 @@ func TestLoadManifest(t *testing.T) {
assert.Assert(t, annotations[UpdateTimestampAnnotationName] != "", "Missing %s annotation", UpdateTimestampAnnotationName)
assert.Assert(t, annotations[UpdateShaAnnotationName] == commitSha, "Expected %s SHA, got %s", commitSha, annotations[UpdateShaAnnotationName])
}