Skip to content

Commit

Permalink
feat: Add conditional to check if the service should expose an extern…
Browse files Browse the repository at this point in the history
…al endpoint
  • Loading branch information
amanvr committed Oct 18, 2023
1 parent a4a20f1 commit ea15793
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 20 deletions.
4 changes: 4 additions & 0 deletions assets/knative/service.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ kind: Service
metadata:
name: {{ .Name }}
namespace: default
{{ if not .IsPublicService }}
labels:
networking.knative.dev/visibility=cluster-local
{{ end }}
spec:
template:
spec:
Expand Down
10 changes: 9 additions & 1 deletion src/cli/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ func (c *icli) Deploy(cCtx *cli.Context) error {
return err
}

return knative.Apply(cCtx.String(namespaceFlag), commitSha, config, project, c.dockerImage, cCtx.String(envVarFileFlag))
return knative.Apply(
cCtx.String(namespaceFlag),
commitSha,
config,
project,
c.dockerImage,
cCtx.String(envVarFileFlag),
cCtx.Bool(publicServiceFlag),
)
}

func (c icli) DeployCMD() *cli.Command {
Expand Down
8 changes: 8 additions & 0 deletions src/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const (
stopOnBuildFlag string = "stop-on-build"
stopOnPushFlag string = "stop-on-push"
envVarFileFlag string = "env-var-file"
publicServiceFlag string = "public-service"
)

type flags struct {
Expand Down Expand Up @@ -114,6 +115,13 @@ func InitFlags() flags {
Value: defaults.EnvVarFile,
Category: "deploy",
},
&cli.BoolFlag{
Name: publicServiceFlag,
EnvVars: []string{"INITIUM_PUBLIC_SERVICE"},
Required: false,
Value: true,
Category: "deploy",
},
},
Registry: []cli.Flag{
&cli.StringFlag{
Expand Down
41 changes: 26 additions & 15 deletions src/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,32 @@ func (c icli) InitConfigCMD(ctx *cli.Context) error {
config := ""
for _, flag := range f {
switch flag.(type) {
case *cli.StringFlag:
stringFlag := flag.(*cli.StringFlag)
if slices.Contains(excludedFlags, stringFlag.Name) {
continue
}

n = stringFlag.Name
v = ctx.String(stringFlag.Name)
case *cli.StringSliceFlag:
stringSliceFlag := flag.(*cli.StringSliceFlag)
if slices.Contains(excludedFlags, stringSliceFlag.Name) {
continue
}
n = stringSliceFlag.Name
v = strings.Join(ctx.StringSlice(stringSliceFlag.Name), ",")
case *cli.StringFlag:
stringFlag := flag.(*cli.StringFlag)
if slices.Contains(excludedFlags, stringFlag.Name) {
continue
}

n = stringFlag.Name
v = ctx.String(stringFlag.Name)
case *cli.StringSliceFlag:
stringSliceFlag := flag.(*cli.StringSliceFlag)
if slices.Contains(excludedFlags, stringSliceFlag.Name) {
continue
}
n = stringSliceFlag.Name
v = strings.Join(ctx.StringSlice(stringSliceFlag.Name), ",")
case *cli.BoolFlag:
boolFlag := flag.(*cli.BoolFlag)
if slices.Contains(excludedFlags, boolFlag.Name) {
continue
}
n = boolFlag.Name
if ctx.Bool(boolFlag.Name) {
v = "true"
} else {
v = "false"
}
}

if v == "" {
Expand Down
7 changes: 4 additions & 3 deletions src/services/k8s/knative.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func Config(endpoint string, token string, caCrt []byte) (*rest.Config, error) {
}, nil
}

func loadManifest(namespace string, commitSha string, project *project.Project, dockerImage docker.DockerImage, envFile string) (*servingv1.Service, error) {
func loadManifest(namespace string, commitSha string, project *project.Project, dockerImage docker.DockerImage, envFile string, isPublicService bool) (*servingv1.Service, error) {
knativeTemplate := path.Join("assets", "knative", "service.yaml.tmpl")
template, err := template.ParseFS(project.Resources, knativeTemplate)
if err != nil {
Expand All @@ -57,6 +57,7 @@ func loadManifest(namespace string, commitSha string, project *project.Project,
"Name": dockerImage.Name,
"RemoteTag": dockerImage.RemoteTag(),
"ImagePullSecrets": project.ImagePullSecrets,
"IsPublicService": isPublicService,
}

output := &bytes.Buffer{}
Expand Down Expand Up @@ -156,12 +157,12 @@ func loadEnvFile(envFile string) ([]corev1.EnvVar, error) {
return envVarList, nil
}

func Apply(namespace string, commitSha string, config *rest.Config, project *project.Project, dockerImage docker.DockerImage, envFile string) error {
func Apply(namespace string, commitSha string, config *rest.Config, project *project.Project, dockerImage docker.DockerImage, envFile string, isPublicService bool) error {
log.Info("Deploying Knative service", "host", config.Host, "name", project.Name, "namespace", namespace)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
defer cancel()

serviceManifest, err := loadManifest(namespace, commitSha, project, dockerImage, envFile)
serviceManifest, err := loadManifest(namespace, commitSha, project, dockerImage, envFile, isPublicService)
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion src/services/k8s/knative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ func TestLoadManifest(t *testing.T) {
commitSha := "93f4be93"
imagePullSecrets := []string{"secretPassword123"}

isPublicService = false

proj := &project.Project{Name: "knative_test",
Directory: path.Join(root, "example"),
Resources: os.DirFS(root),
Expand All @@ -73,7 +75,7 @@ func TestLoadManifest(t *testing.T) {
Tag: "v1.1.0",
}

serviceManifest, err := loadManifest(namespace, commitSha, proj, dockerImage, path.Join(root, "example/.env.sample"))
serviceManifest, err := loadManifest(namespace, commitSha, proj, dockerImage, path.Join(root, "example/.env.sample"), isPublicService)

if err != nil {
t.Fatalf(fmt.Sprintf("Error: %v", err))
Expand Down

0 comments on commit ea15793

Please sign in to comment.