Skip to content

Commit

Permalink
Update hybrid plugin scaffolding (#99)
Browse files Browse the repository at this point in the history
* Update hybrid plugin scaffolding

This commit:
1. Adds additional customizations to be done in config/ as
   post-scaffolding step in init command.
2. Modify Dockerfile template.
3. Modify main.go to accept additional flags for watches.yaml and
   leader-election-id.
4. creates placeholder directories

Signed-off-by: varshaprasad96 <varshaprasad96@gmail.com>
  • Loading branch information
varshaprasad96 committed Aug 5, 2021
1 parent 51e53f5 commit 7cb02dd
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 11 deletions.
61 changes: 61 additions & 0 deletions pkg/plugins/hybrid/v1alpha/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ package v1alpha

import (
"fmt"
"path/filepath"

"github.com/operator-framework/helm-operator-plugins/pkg/plugins/hybrid/v1alpha/scaffolds"
projutil "github.com/operator-framework/helm-operator-plugins/pkg/plugins/util"
"github.com/spf13/pflag"
"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
Expand Down Expand Up @@ -100,6 +102,10 @@ func (p *initSubcommand) InjectConfig(c config.Config) error {
// helm operator.
func (p *initSubcommand) Scaffold(fs machinery.Filesystem) error {

if err := addInitCustomizations(p.config.GetProjectName()); err != nil {
return fmt.Errorf("error updating init manifests: %q", err)
}

scaffolder := scaffolds.NewInitScaffolder(p.config, p.license, p.owner)
scaffolder.InjectFS(fs)
err := scaffolder.Scaffold()
Expand All @@ -125,3 +131,58 @@ func (p *initSubcommand) PostScaffold() error {

return nil
}

// addInitCustomizations will perform the required customizations for this plugin on the common base
func addInitCustomizations(projectName string) error {
managerFile := filepath.Join("config", "manager", "manager.yaml")

// todo: we ought to use afero instead. Replace this methods to insert/update
// by https://github.com/kubernetes-sigs/kubebuilder/pull/2119

// Add leader election arg in config/manager/manager.yaml and in config/default/manager_auth_proxy_patch.yaml
err := projutil.InsertCode(managerFile,
"--leader-elect",
fmt.Sprintf("\n - --leader-election-id=%s", projectName))
if err != nil {
return err
}
err = projutil.InsertCode(filepath.Join("config", "default", "manager_auth_proxy_patch.yaml"),
"- \"--leader-elect\"",
fmt.Sprintf("\n - \"--leader-election-id=%s\"", projectName))
if err != nil {
return err
}

// Increase the default memory required.
err = projutil.ReplaceInFile(managerFile, "memory: 30Mi", "memory: 90Mi")
if err != nil {
return err
}
err = projutil.ReplaceInFile(managerFile, "memory: 20Mi", "memory: 60Mi")
if err != nil {
return err
}

// Remove the webhook option for the componentConfig since webhooks are not supported by helm
err = projutil.ReplaceInFile(filepath.Join("config", "manager", "controller_manager_config.yaml"),
"webhook:\n port: 9443", "")
if err != nil {
return err
}

// Remove the call to the command as manager. Helm has not been exposing this entrypoint
// todo: provide the manager entrypoint for helm and then remove it
const command = `command:
- /manager
`
err = projutil.ReplaceInFile(managerFile, command, "")
if err != nil {
return err
}

if err := projutil.UpdateKustomizationsInit(); err != nil {
return fmt.Errorf("error updating kustomization.yaml files: %v", err)
}

return nil
}
17 changes: 17 additions & 0 deletions pkg/plugins/hybrid/v1alpha/scaffolds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ package scaffolds

import (
"fmt"
"os"

"github.com/operator-framework/helm-operator-plugins/pkg/plugins/hybrid/v1alpha/scaffolds/internal/templates"
"github.com/operator-framework/helm-operator-plugins/pkg/plugins/hybrid/v1alpha/scaffolds/internal/templates/hack"
"github.com/operator-framework/helm-operator-plugins/pkg/plugins/hybrid/v1alpha/scaffolds/internal/templates/rbac"
"github.com/operator-framework/helm-operator-plugins/pkg/plugins/v1/chartutil"
"github.com/spf13/afero"
"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
Expand Down Expand Up @@ -108,6 +110,12 @@ func (s *initScaffolder) Scaffold() error {
machinery.WithBoilerplate(string(boilerplate)),
)

// create placeholder directories for helm charts and go apis
err = createDirectories([]string{chartutil.HelmChartsDir, "api", "controllers"})
if err != nil {
return err
}

err = scaffold.Execute(
&templates.Main{},
&templates.GoMod{ControllerRuntimeVersion: ControllerRuntimeVersion},
Expand Down Expand Up @@ -137,3 +145,12 @@ func (s *initScaffolder) Scaffold() error {

return nil
}

func createDirectories(directories []string) error {
for _, dir := range directories {
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("unable to create directory %q : %v", dir, err)
}
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,26 @@ RUN go mod download
# Copy the go source
COPY main.go main.go
COPY api/ api/
COPY controllers/ controllers/
# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go
# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM gcr.io/distroless/static:nonroot
WORKDIR /
FROM registry.access.redhat.com/ubi8/ubi-minimal:8.4
ENV HOME=/opt/helm
ENV HOME=/opt/helm \
USER_ID=65532
# Copy necessary files
COPY watches.yaml ${HOME}/watches.yaml
COPY helm-charts ${HOME}/helm-charts
# Copy necessary files with the right permissions
COPY --chown=${USER_ID}:${USER_ID} watches.yaml ${HOME}/watches.yaml
COPY --chown=${USER_ID}:${USER_ID} helm-charts ${HOME}/helm-charts
# Copy manager binary
COPY --from=builder /workspace/manager .
USER 65532:65532
WORKDIR ${HOME}
ENTRYPOINT ["/manager"]
`
11 changes: 8 additions & 3 deletions pkg/plugins/hybrid/v1alpha/scaffolds/internal/templates/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,11 @@ func (f *MainUpdater) GetCodeFragments() machinery.CodeFragmentsMap {
return fragments
}

// TODO: this template would be modified when pkg/watches is updated according to the latest
// TODO:
// 1. Modify the template when pkg/watches is updated according to the latest
// helm v1 plugin in operator-sdk.
// 2. Move the flags to a seprate package and clean up the scaffolding.
// 3. Modify leader-election-id to be the project name.
var mainTemplate = `{{ .Boilerplate }}
package main
Expand Down Expand Up @@ -226,6 +229,8 @@ func main() {
var probeAddr string
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.StringVar(&watchesPath, "watches-file", "watches.yaml", "path to watches file")
flag.StringVar(&leaderElectionID, "leader-election-id", "{{ hashFNV .Repo }}.{{ .Domain }}", "provide leader election")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. " +
"Enabling this will ensure there is only one active controller manager.")
Expand All @@ -251,7 +256,7 @@ func main() {
Port: 9443,
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "{{ hashFNV .Repo }}.{{ .Domain }}",
LeaderElectionID: leaderElectionID,
})
{{- else }}
var err error
Expand Down Expand Up @@ -282,7 +287,7 @@ func main() {
os.Exit(1)
}
ws, err := watches.Load("./watches.yaml")
ws, err := watches.Load(watchesPath)
if err != nil {
setupLog.Error(err, "Failed to create new manager factories")
os.Exit(1)
Expand Down

0 comments on commit 7cb02dd

Please sign in to comment.