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

Update hybrid plugin scaffolding #99

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
}
9 changes: 9 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,10 @@ func (s *initScaffolder) Scaffold() error {
machinery.WithBoilerplate(string(boilerplate)),
)

if err := os.MkdirAll(chartutil.HelmChartsDir, 0755); err != nil {
return err
}

err = scaffold.Execute(
&templates.Main{},
&templates.GoMod{ControllerRuntimeVersion: ControllerRuntimeVersion},
Expand All @@ -125,6 +131,9 @@ func (s *initScaffolder) Scaffold() error {
&templates.DockerIgnore{},
)

// Add note to include any depedencies in dockerfile which are required to build `main.go`.
fmt.Println("Include any dependencies required to build `main.go` in your project to Dockerfile")
varshaprasad96 marked this conversation as resolved.
Show resolved Hide resolved

if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,20 @@ COPY main.go main.go
# 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