Skip to content

Commit

Permalink
ansible/helm : aling flags and deprecated the old ones
Browse files Browse the repository at this point in the history
Signed-off-by: Camila Macedo <cmacedo@redhat.com>
  • Loading branch information
Camila Macedo committed Mar 13, 2021
1 parent c6796de commit 1b810a7
Show file tree
Hide file tree
Showing 29 changed files with 169 additions and 456 deletions.
15 changes: 15 additions & 0 deletions internal/ansible/flags/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ type Flags struct {
WatchesFile string
InjectOwnerRef bool
EnableLeaderElection bool
LeaderElection bool
MaxConcurrentReconciles int
AnsibleVerbosity int
AnsibleRolesPath string
AnsibleCollectionsPath string
MetricsAddress string
MetricsBindAddress string
ProbeAddr string
LeaderElectionID string
LeaderElectionNamespace string
Expand Down Expand Up @@ -84,6 +86,12 @@ func (f *Flags) AddTo(flagSet *pflag.FlagSet) {
":8080",
"The address the metric endpoint binds to",
)
flagSet.MarkDeprecated("metrics-addr", "Use the flag metrics-bind-address instead of")
flagSet.StringVar(&f.MetricsBindAddress,
"metrics-bind-address",
":8080",
"The address the metric endpoint binds to",
)
// todo: for Go/Helm the port used is: 8081
// update it to keep the project aligned to the other
// types for 2.0
Expand All @@ -98,6 +106,13 @@ func (f *Flags) AddTo(flagSet *pflag.FlagSet) {
"Enable leader election for controller manager. Enabling this will"+
" ensure there is only one active controller manager.",
)
flagSet.MarkDeprecated("enable-leader-election", "Use the flag leader-elect instead of.")
flagSet.BoolVar(&f.LeaderElection,
"leader-elect",
false,
"Enable leader election for controller manager. Enabling this will"+
" ensure there is only one active controller manager.",
)
flagSet.StringVar(&f.LeaderElectionID,
"leader-election-id",
"",
Expand Down
18 changes: 16 additions & 2 deletions internal/cmd/ansible-operator/run/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,26 @@ func run(cmd *cobra.Command, f *flags.Flags) {
}
}

// We need to check both because EnableLeaderElection is deprecated
// If one of them is true than it means that the default value was changed
// todo:remove it for 2.0.0
if f.LeaderElection || f.EnableLeaderElection {
f.LeaderElection = true
}

// If the deprecated flag --metrics-addr was used it will not
// be equals the default value.
// todo:remove it for 2.0.0
if f.MetricsAddress != ":8080" {
f.MetricsBindAddress = f.MetricsAddress
}

// Set default manager options
// TODO: probably should expose the host & port as an environment variables
options := manager.Options{
MetricsBindAddress: f.MetricsAddress,
MetricsBindAddress: f.MetricsBindAddress,
HealthProbeBindAddress: f.ProbeAddr,
LeaderElection: f.EnableLeaderElection,
LeaderElection: f.LeaderElection,
LeaderElectionID: f.LeaderElectionID,
LeaderElectionResourceLock: resourcelock.ConfigMapsResourceLock,
LeaderElectionNamespace: f.LeaderElectionNamespace,
Expand Down
23 changes: 19 additions & 4 deletions internal/cmd/helm-operator/run/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ func run(cmd *cobra.Command, f *flags.Flags) {
os.Exit(1)
}

// Deprecated: OPERATOR_NAME environment variable is an artifact of the legacy operator-sdk project scaffolding.
// Flag `--leader-election-id` should be used instead.
// Deprecated: OPERATOR_NAME environment variable is an artifact of the
// legacy operator-sdk project scaffolding. Flag `--leader-election-id`
// should be used instead.
if operatorName, found := os.LookupEnv("OPERATOR_NAME"); found {
log.Info("Environment variable OPERATOR_NAME has been deprecated, use --leader-election-id instead.")
if cmd.Flags().Lookup("leader-election-id").Changed {
Expand All @@ -95,11 +96,25 @@ func run(cmd *cobra.Command, f *flags.Flags) {
}
}

// We need to check both because EnableLeaderElection is deprecated
// If one of them is true than it means that the default value was changed
// todo:remove it for 2.0.0
if f.LeaderElection || f.EnableLeaderElection {
f.LeaderElection = true
}

// If the deprecated flag --metrics-addr was used it will not
// be equals the default value.
// todo:remove it for 2.0.0
if f.MetricsAddress != ":8080" {
f.MetricsBindAddress = f.MetricsAddress
}

// Set default manager options
options := manager.Options{
MetricsBindAddress: f.MetricsAddress,
MetricsBindAddress: f.MetricsBindAddress,
HealthProbeBindAddress: f.ProbeAddr,
LeaderElection: f.EnableLeaderElection,
LeaderElection: f.LeaderElection,
LeaderElectionID: f.LeaderElectionID,
LeaderElectionResourceLock: resourcelock.ConfigMapsResourceLock,
LeaderElectionNamespace: f.LeaderElectionNamespace,
Expand Down
25 changes: 23 additions & 2 deletions internal/helm/flags/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ type Flags struct {
ReconcilePeriod time.Duration
WatchesFile string
MetricsAddress string
MetricsBindAddress string
EnableLeaderElection bool
LeaderElection bool
LeaderElectionID string
LeaderElectionNamespace string
MaxConcurrentReconciles int
Expand All @@ -50,6 +52,15 @@ func (f *Flags) AddTo(flagSet *pflag.FlagSet) {
":8080",
"The address the metric endpoint binds to",
)
flagSet.MarkDeprecated("metrics-addr", "Use the flag metrics-bind-address instead of")
flagSet.StringVar(&f.MetricsBindAddress,
"metrics-bind-address",
":8080",
"The address the metric endpoint binds to",
)
// todo: for Go/Helm the port used is: 8081
// update it to keep the project aligned to the other
// types for 2.0
flagSet.StringVar(&f.ProbeAddr,
"health-probe-bind-address",
":8081",
Expand All @@ -58,7 +69,15 @@ func (f *Flags) AddTo(flagSet *pflag.FlagSet) {
flagSet.BoolVar(&f.EnableLeaderElection,
"enable-leader-election",
false,
"Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.",
"Enable leader election for controller manager. Enabling this will"+
" ensure there is only one active controller manager.",
)
flagSet.MarkDeprecated("enable-leader-election", "Use the flag leader-elect instead of.")
flagSet.BoolVar(&f.LeaderElection,
"leader-elect",
false,
"Enable leader election for controller manager. Enabling this will"+
" ensure there is only one active controller manager.",
)
flagSet.StringVar(&f.LeaderElectionID,
"leader-election-id",
Expand All @@ -68,7 +87,9 @@ func (f *Flags) AddTo(flagSet *pflag.FlagSet) {
flagSet.StringVar(&f.LeaderElectionNamespace,
"leader-election-namespace",
"",
"Namespace in which to create the leader election configmap for holding the leader lock (required if running locally with leader election enabled).",
"Namespace in which to create the leader election configmap for"+
" holding the leader lock (required if running locally with leader"+
" election enabled).",
)
flagSet.IntVar(&f.MaxConcurrentReconciles,
"max-concurrent-reconciles",
Expand Down
6 changes: 3 additions & 3 deletions internal/plugins/ansible/v1/scaffolds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ func (s *initScaffolder) scaffold() error {
&prometheus.Kustomization{},
&prometheus.ServiceMonitor{},

&manager.Manager{Image: imageName},
&manager.Config{Image: imageName},
&manager.Kustomization{},

&kdefault.Kustomize{},
&kdefault.AuthProxyPatch{},
&kdefault.Kustomization{},
&kdefault.ManagerAuthProxyPatch{},

&roles.Placeholder{},
&playbooks.Placeholder{},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ import (
"sigs.k8s.io/kubebuilder/v3/pkg/model/file"
)

var _ file.Template = &Kustomize{}
var _ file.Template = &Kustomization{}

// Kustomize scaffolds the Kustomization file for the default overlay
type Kustomize struct {
// Kustomization scaffolds a file that defines the kustomization scheme for the default overlay folder
type Kustomization struct {
file.TemplateMixin
file.ProjectNameMixin
}

// SetTemplateDefaults implements input.Template
func (f *Kustomize) SetTemplateDefaults() error {
// SetTemplateDefaults implements file.Template
func (f *Kustomization) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("config", "default", "kustomization.yaml")
}
Expand Down Expand Up @@ -66,8 +66,8 @@ bases:
#- ../prometheus
patchesStrategicMerge:
# Protect the /metrics endpoint by putting it behind auth.
# If you want your controller-manager to expose the /metrics
# endpoint w/o any authn/z, please comment the following line.
# Protect the /metrics endpoint by putting it behind auth.
# If you want your controller-manager to expose the /metrics
# endpoint w/o any authn/z, please comment the following line.
- manager_auth_proxy_patch.yaml
`
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@ import (
"sigs.k8s.io/kubebuilder/v3/pkg/model/file"
)

var _ file.Template = &AuthProxyPatch{}
var _ file.Template = &ManagerAuthProxyPatch{}

// AuthProxyPatch scaffolds the patch file for enabling
// prometheus metrics for manager Pod.
type AuthProxyPatch struct {
// ManagerAuthProxyPatch scaffolds a file that defines the patch that enables prometheus metrics for the manager
type ManagerAuthProxyPatch struct {
file.TemplateMixin
file.ProjectNameMixin
}

// SetTemplateDefaults implements input.Template
func (f *AuthProxyPatch) SetTemplateDefaults() error {
// SetTemplateDefaults implements file.Template
func (f *ManagerAuthProxyPatch) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("config", "default", "manager_auth_proxy_patch.yaml")
}
Expand Down Expand Up @@ -68,7 +67,8 @@ spec:
name: https
- name: manager
args:
- "--metrics-addr=127.0.0.1:8080"
- "--enable-leader-election"
- "--health-probe-bind-address=:8081"
- "--metrics-bind-address=127.0.0.1:8080"
- "--leader-elect"
- "--leader-election-id={{ .ProjectName }}"
`
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
Copyright 2018 The Kubernetes Authors.
Modifications copyright 2020 The Operator-SDK Authors
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -23,19 +22,19 @@ import (
"sigs.k8s.io/kubebuilder/v3/pkg/model/file"
)

var _ file.Template = &Manager{}
var _ file.Template = &Config{}

// Manager scaffolds yaml config for the manager.
type Manager struct {
// Config scaffolds a file that defines the namespace and the manager deployment
type Config struct {
file.TemplateMixin
file.ProjectNameMixin

// Image is controller manager image name
Image string
}

// SetTemplateDefaults implements input.Template
func (f *Manager) SetTemplateDefaults() error {
// SetTemplateDefaults implements file.Template
func (f *Config) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("config", "manager", "manager.yaml")
}
Expand Down Expand Up @@ -72,7 +71,7 @@ spec:
containers:
- name: manager
args:
- "--enable-leader-election"
- "--leader-elect"
- "--leader-election-id={{ .ProjectName }}"
env:
- name: ANSIBLE_GATHERING
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import (

var _ file.Template = &Kustomization{}

// Kustomization scaffolds the Kustomization file in manager folder.
// Kustomization scaffolds a file that defines the kustomization scheme for the manager folder
type Kustomization struct {
file.TemplateMixin
}

// SetTemplateDefaults implements input.Template
// SetTemplateDefaults implements file.Template
func (f *Kustomization) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("config", "manager", "kustomization.yaml")
Expand Down
4 changes: 2 additions & 2 deletions internal/plugins/helm/v1/scaffolds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ func (s *initScaffolder) scaffold() error {
&rbac.ManagerRole{},
&rbac.ManagerRoleBinding{},
&manager.Kustomization{},
&manager.Manager{Image: imageName},
&manager.Config{Image: imageName},
&prometheus.Kustomization{},
&prometheus.ServiceMonitor{},
&kdefault.AuthProxyPatch{},
&kdefault.ManagerAuthProxyPatch{},
&kdefault.Kustomization{},
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Kustomization struct {
file.ProjectNameMixin
}

// SetTemplateDefaults implements input.Template
// SetTemplateDefaults implements file.Template
func (f *Kustomization) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("config", "default", "kustomization.yaml")
Expand Down Expand Up @@ -66,8 +66,8 @@ bases:
#- ../prometheus
patchesStrategicMerge:
# Protect the /metrics endpoint by putting it behind auth.
# If you want your controller-manager to expose the /metrics
# endpoint w/o any authn/z, please comment the following line.
# Protect the /metrics endpoint by putting it behind auth.
# If you want your controller-manager to expose the /metrics
# endpoint w/o any authn/z, please comment the following line.
- manager_auth_proxy_patch.yaml
`
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@ import (
"sigs.k8s.io/kubebuilder/v3/pkg/model/file"
)

var _ file.Template = &AuthProxyPatch{}
var _ file.Template = &ManagerAuthProxyPatch{}

// AuthProxyPatch scaffolds the patch file for enabling
// prometheus metrics for manager Pod.
type AuthProxyPatch struct {
// ManagerAuthProxyPatch scaffolds a file that defines the patch that enables prometheus metrics for the manager
type ManagerAuthProxyPatch struct {
file.TemplateMixin
file.ProjectNameMixin
}

// SetTemplateDefaults implements input.Template
func (f *AuthProxyPatch) SetTemplateDefaults() error {
// SetTemplateDefaults implements file.Template
func (f *ManagerAuthProxyPatch) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("config", "default", "manager_auth_proxy_patch.yaml")
}
Expand Down Expand Up @@ -68,7 +67,8 @@ spec:
name: https
- name: manager
args:
- "--metrics-addr=127.0.0.1:8080"
- "--enable-leader-election"
- "--leader-election-id={{ .ProjectName }}"
- "--health-probe-bind-address=:8081"
- "--metrics-bind-address=127.0.0.1:8080"
- "--leader-elect"
- "--leader-election-id={{ .ProjectName }}"- "--leader-election-id={{ .ProjectName }}"
`
Loading

0 comments on commit 1b810a7

Please sign in to comment.