Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Add install opt to set annotations on acorn-api pods
Browse files Browse the repository at this point in the history
Enable custom annotations to be set on acorn-api pods by adding an
`--api-server-pod-annotations` option to `acorn install`.

Signed-off-by: Nick Hale <4175918+njhale@users.noreply.github.com>
  • Loading branch information
njhale committed Aug 23, 2023
1 parent 37cd895 commit 0517559
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 17 deletions.
31 changes: 23 additions & 8 deletions pkg/cli/install.go
Expand Up @@ -35,6 +35,7 @@ type Install struct {
Output string `usage:"Output manifests instead of applying them (json, yaml)" short:"o"`

APIServerReplicas *int `usage:"acorn-api deployment replica count" name:"api-server-replicas"`
APIServerPodAnnotations []string `usage:"annotations to apply to acorn-api pods" name:"api-server-pod-annotations" split:"false"`
ControllerReplicas *int `usage:"acorn-controller deployment replica count"`
ControllerServiceAccountAnnotation []string `usage:"annotation to apply to the acorn-system service account"`

Expand Down Expand Up @@ -83,22 +84,24 @@ func (i *Install) Run(cmd *cobra.Command, args []string) error {
image = i.Image
}

annotations := map[string]string{}
for _, anno := range i.ControllerServiceAccountAnnotation {
k, v, ok := strings.Cut(anno, "=")
if !ok {
return fmt.Errorf("--controller-service-account-annotation must be in key=value format got [%s]", anno)
}
annotations[k] = v
controllerSAAnnotations, err := parseAnnotations(i.ControllerServiceAccountAnnotation)
if err != nil {
return fmt.Errorf("invalid --controller-service-account-annotation %w", err)
}

apiPodAnnotations, err := parseAnnotations(i.APIServerPodAnnotations)
if err != nil {
return fmt.Errorf("invalid --api-server-pod-annotations: %w", err)
}

opts := &install.Options{
SkipChecks: i.SkipChecks,
OutputFormat: i.Output,
Config: i.Config,
APIServerReplicas: i.APIServerReplicas,
APIServerPodAnnotations: apiPodAnnotations,
ControllerReplicas: i.ControllerReplicas,
ControllerServiceAccountAnnotations: annotations,
ControllerServiceAccountAnnotations: controllerSAAnnotations,
}

if i.Dev != "" {
Expand All @@ -107,3 +110,15 @@ func (i *Install) Run(cmd *cobra.Command, args []string) error {

return install.Install(cmd.Context(), image, opts)
}

func parseAnnotations(annotations []string) (map[string]string, error) {
result := make(map[string]string, len(annotations))
for _, anno := range annotations {
k, v, ok := strings.Cut(anno, "=")
if !ok {
return nil, fmt.Errorf("annotation must be in key=value format got [%s]", anno)
}
result[k] = v
}
return result, nil
}
47 changes: 38 additions & 9 deletions pkg/install/install.go
Expand Up @@ -61,6 +61,7 @@ type Options struct {
SkipChecks bool
OutputFormat string
APIServerReplicas *int
APIServerPodAnnotations map[string]string
ControllerReplicas *int
ControllerServiceAccountAnnotations map[string]string
Config apiv1.Config
Expand Down Expand Up @@ -251,7 +252,7 @@ func Install(ctx context.Context, image string, opts *Options) error {

s = opts.Progress.New(fmt.Sprintf("Installing APIServer and Controller (image %s)", image))
if err := applyDeployments(ctx, image, *opts.APIServerReplicas, *opts.ControllerReplicas, *opts.Config.UseCustomCABundle,
opts.ControllerServiceAccountAnnotations, apply, c); err != nil {
opts.ControllerServiceAccountAnnotations, opts.APIServerPodAnnotations, apply, c); err != nil {
return s.Fail(err)
}
s.Success()
Expand Down Expand Up @@ -467,7 +468,7 @@ func resources(image string, opts *Options) ([]kclient.Object, error) {
objs = append(objs, namespace...)

deps, err := Deployments(image, *opts.APIServerReplicas, *opts.ControllerReplicas, *opts.Config.UseCustomCABundle,
opts.ControllerServiceAccountAnnotations)
opts.ControllerServiceAccountAnnotations, opts.APIServerPodAnnotations)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -508,7 +509,7 @@ func printObject(image string, opts *Options) error {
return err
}

func applyDeployments(ctx context.Context, imageName string, apiServerReplicas, controllerReplicas int, useCustomCABundle bool, annotations map[string]string, apply apply.Apply, c kclient.Client) error {
func applyDeployments(ctx context.Context, imageName string, apiServerReplicas, controllerReplicas int, useCustomCABundle bool, controllerSAAnnotations, apiPodAnnotations map[string]string, apply apply.Apply, c kclient.Client) error {
// handle upgrade from <= v0.3.x
if err := resetNamespace(ctx, c); err != nil {
return err
Expand All @@ -519,7 +520,7 @@ func applyDeployments(ctx context.Context, imageName string, apiServerReplicas,
return err
}

deps, err := Deployments(imageName, apiServerReplicas, controllerReplicas, useCustomCABundle, annotations)
deps, err := Deployments(imageName, apiServerReplicas, controllerReplicas, useCustomCABundle, controllerSAAnnotations, apiPodAnnotations)
if err != nil {
return err
}
Expand Down Expand Up @@ -559,7 +560,7 @@ func Namespace() ([]kclient.Object, error) {
return objectsFromFile("namespace.yaml")
}

func Deployments(runtimeImage string, apiServerReplicas, controllerReplicas int, useCustomCABundle bool, annotations map[string]string) ([]kclient.Object, error) {
func Deployments(runtimeImage string, apiServerReplicas, controllerReplicas int, useCustomCABundle bool, controllerSAAnnotations, apiPodAnnotations map[string]string) ([]kclient.Object, error) {
apiServerObjects, err := objectsFromFile("apiserver.yaml")
if err != nil {
return nil, err
Expand All @@ -575,12 +576,17 @@ func Deployments(runtimeImage string, apiServerReplicas, controllerReplicas int,
return nil, err
}

apiServerObjects, err = replacePodAnnotations(apiPodAnnotations, apiServerObjects)
if err != nil {
return nil, err
}

controllerObjects, err = replaceReplicas(controllerReplicas, controllerObjects)
if err != nil {
return nil, err
}

controllerObjects, err = replaceAnnotations(annotations, controllerObjects)
controllerObjects, err = replaceSAAnnotations(controllerSAAnnotations, controllerObjects)
if err != nil {
return nil, err
}
Expand All @@ -597,16 +603,39 @@ func Deployments(runtimeImage string, apiServerReplicas, controllerReplicas int,
return replaceImage(runtimeImage, objects)
}

func replaceAnnotations(annotations map[string]string, objs []kclient.Object) ([]kclient.Object, error) {
val := map[string]any{}
func replacePodAnnotations(annotations map[string]string, objs []kclient.Object) ([]kclient.Object, error) {
if len(annotations) == 0 {
return objs, nil
}

val := make(map[string]any, len(annotations))
for k, v := range annotations {
val[k] = v
}

if len(val) == 0 {
for _, obj := range objs {
ustr := obj.(*unstructured.Unstructured)
if ustr.GetKind() == "Deployment" {
err := unstructured.SetNestedField(ustr.Object, val, "spec", "template", "metadata", "annotations")
if err != nil {
return nil, err
}
}
}

return objs, nil
}

func replaceSAAnnotations(annotations map[string]string, objs []kclient.Object) ([]kclient.Object, error) {
if len(annotations) == 0 {
return objs, nil
}

val := make(map[string]any, len(annotations))
for k, v := range annotations {
val[k] = v
}

for _, obj := range objs {
ustr := obj.(*unstructured.Unstructured)
if ustr.GetKind() == "ServiceAccount" {
Expand Down

0 comments on commit 0517559

Please sign in to comment.