Skip to content
This repository was archived by the owner on Apr 11, 2024. It is now read-only.

Commit a856a1f

Browse files
authored
feat: Single defaults namespace flag (nutanix-cloud-native#426)
1 parent 833b1ba commit a856a1f

File tree

16 files changed

+116
-114
lines changed

16 files changed

+116
-114
lines changed

charts/capi-runtime-extensions/templates/deployment.yaml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,9 @@ spec:
2929
imagePullPolicy: "{{ .Values.image.pullPolicy }}"
3030
args:
3131
- --webhook-cert-dir=/runtimehooks-certs/
32-
- --cni.calico.crs.defaults-namespace={{ .Release.Namespace }}
33-
- --cni.calico.helm-addon.defaults-namespace={{ .Release.Namespace }}
34-
- --cni.cilium.crs.defaults-namespace={{ .Release.Namespace }}
35-
- --cni.cilium.helm-addon.defaults-namespace={{ .Release.Namespace }}
32+
- --defaults-namespace=$(POD_NAMESPACE)
3633
- --cni.cilium.helm-addon.default-values-template-configmap-name={{ .Values.hooks.cni.cilium.helmAddonStrategy.defaultValueTemplateConfigMap.name }}
37-
- --nfd.crs.defaults-namespace={{ .Release.Namespace }}
38-
- --nfd.helm-addon.defaults-namespace={{ .Release.Namespace }}
3934
- --nfd.helm-addon.default-values-template-configmap-name={{ .Values.hooks.nfd.helmAddonStrategy.defaultValueTemplateConfigMap.name }}
40-
- --awscpi.defaults-namespace={{ .Release.Namespace }}
41-
- --awsebs.defaults-namespace={{ .Release.Namespace }}
4235
{{- range $key, $value := .Values.extraArgs }}
4336
- --{{ $key }}={{ $value }}
4437
{{- end }}
@@ -58,6 +51,11 @@ spec:
5851
- containerPort: 8081
5952
name: probes
6053
protocol: TCP
54+
env:
55+
- name: POD_NAMESPACE
56+
valueFrom:
57+
fieldRef:
58+
fieldPath: metadata.namespace
6159
resources:
6260
{{ with .Values.resources }}
6361
{{- toYaml . | nindent 10 }}

cmd/main.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,9 @@ import (
3434
dockermutation "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/docker/mutation"
3535
dockerworkerconfig "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/docker/workerconfig"
3636
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/lifecycle"
37+
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/options"
3738
)
3839

39-
// Flags.
40-
var logOptions = logs.NewOptions()
41-
42-
// initFlags initializes the flags.
43-
func initFlags(fs *pflag.FlagSet) {
44-
// Initialize logs flags using Kubernetes component-base machinery.
45-
logs.AddFlags(fs, logs.SkipLoggingConfigurationFlags())
46-
logsv1.AddFlags(logOptions, fs)
47-
}
48-
4940
func main() {
5041
// Creates a logger to be used during the main func.
5142
setupLog := ctrl.Log.WithName("main")
@@ -82,12 +73,18 @@ func main() {
8273
pflag.CommandLine.StringVar(&mgrOptions.PprofBindAddress, "profiler-address", "",
8374
"Bind address to expose the pprof profiler (e.g. localhost:6060)")
8475

76+
logOptions := logs.NewOptions()
77+
8578
runtimeWebhookServerOpts := server.NewServerOptions()
8679

87-
genericLifecycleHandlers := lifecycle.New()
80+
globalOptions := options.NewGlobalOptions()
81+
82+
genericLifecycleHandlers := lifecycle.New(globalOptions)
8883

8984
// Initialize and parse command line flags.
90-
initFlags(pflag.CommandLine)
85+
logs.AddFlags(pflag.CommandLine, logs.SkipLoggingConfigurationFlags())
86+
logsv1.AddFlags(logOptions, pflag.CommandLine)
87+
globalOptions.AddFlags(pflag.CommandLine)
9188
runtimeWebhookServerOpts.AddFlags(pflag.CommandLine)
9289
genericLifecycleHandlers.AddFlags(pflag.CommandLine)
9390
pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)

make/kind.mk

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ KIND_CLUSTER_NAME ?= $(GITHUB_REPOSITORY)-dev
77
KIND_KUBECONFIG ?= $(KIND_DIR)/$(KIND_CLUSTER_NAME)/kubeconfig
88

99
KINDEST_NODE_IMAGE ?= ghcr.io/mesosphere/kind-node
10-
KINDEST_NODE_VERSION_v1.26 ?= v1.26.13
11-
KINDEST_NODE_VERSION_v1.27 ?= v1.27.10
12-
KINDEST_NODE_VERSION_v1.28 ?= v1.28.6
13-
KINDEST_NODE_VERSION_v1.29 ?= v1.29.1
10+
KINDEST_NODE_VERSION_v1.26 ?= v1.26.14
11+
KINDEST_NODE_VERSION_v1.27 ?= v1.27.11
12+
KINDEST_NODE_VERSION_v1.28 ?= v1.28.7
13+
KINDEST_NODE_VERSION_v1.29 ?= v1.29.2
1414
# Allow easy override of Kubernetes version to use via `make KIND_KUBERNETES_VERSION=v1.23` to use in CI
15-
KIND_KUBERNETES_VERSION ?= v1.28
15+
KIND_KUBERNETES_VERSION ?= v1.29
1616
ifndef KINDEST_NODE_VERSION_$(KIND_KUBERNETES_VERSION)
1717
$(error Unsupported Kind Kubernetes version: $(KIND_KUBERNETES_VERSION) (use on of: [$(patsubst KINDEST_NODE_VERSION_%,%,$(filter KINDEST_NODE_VERSION_%,$(.VARIABLES)))]))
1818
endif

pkg/handlers/generic/lifecycle/cni/calico/handler.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,21 @@ import (
1818
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/lifecycle"
1919
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/variables"
2020
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/clusterconfig"
21+
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/options"
2122
)
2223

2324
type addonStrategy interface {
24-
apply(context.Context, *runtimehooksv1.AfterControlPlaneInitializedRequest, logr.Logger) error
25+
apply(
26+
context.Context,
27+
*runtimehooksv1.AfterControlPlaneInitializedRequest,
28+
string,
29+
logr.Logger,
30+
) error
2531
}
2632

2733
type CNIConfig struct {
34+
*options.GlobalOptions
35+
2836
crsConfig crsConfig
2937
helmAddonConfig helmAddonConfig
3038
}
@@ -126,7 +134,7 @@ func (s *CalicoCNI) AfterControlPlaneInitialized(
126134
return
127135
}
128136

129-
if err := strategy.apply(ctx, req, log); err != nil {
137+
if err := strategy.apply(ctx, req, s.config.DefaultsNamespace(), log); err != nil {
130138
resp.SetStatus(runtimehooksv1.ResponseStatusFailure)
131139
resp.SetMessage(err.Error())
132140
return

pkg/handlers/generic/lifecycle/cni/calico/strategy_crs.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,11 @@ import (
2727
)
2828

2929
type crsConfig struct {
30-
defaultsNamespace string
31-
3230
defaultTigeraOperatorConfigMapName string
3331
defaultProviderInstallationConfigMapNames map[string]string
3432
}
3533

3634
func (c *crsConfig) AddFlags(prefix string, flags *pflag.FlagSet) {
37-
flags.StringVar(
38-
&c.defaultsNamespace,
39-
prefix+".defaults-namespace",
40-
corev1.NamespaceDefault,
41-
"namespace of the ConfigMap used to deploy Tigera Operator",
42-
)
43-
4435
flags.StringVar(
4536
&c.defaultTigeraOperatorConfigMapName,
4637
prefix+".default-tigera-operator-configmap-name",
@@ -67,6 +58,7 @@ type crsStrategy struct {
6758
func (s crsStrategy) apply(
6859
ctx context.Context,
6960
req *runtimehooksv1.AfterControlPlaneInitializedRequest,
61+
defaultsNamespace string,
7062
log logr.Logger,
7163
) error {
7264
infraKind := req.Cluster.Spec.InfrastructureRef.Kind
@@ -82,7 +74,7 @@ func (s crsStrategy) apply(
8274
}
8375

8476
log.Info("Ensuring Tigera manifests ConfigMap exist in cluster namespace")
85-
tigeraCM, err := s.ensureTigeraOperatorConfigMap(ctx, &req.Cluster)
77+
tigeraCM, err := s.ensureTigeraOperatorConfigMap(ctx, &req.Cluster, defaultsNamespace)
8678
if err != nil {
8779
log.Error(
8880
err,
@@ -98,6 +90,7 @@ func (s crsStrategy) apply(
9890
if err := s.ensureCNICRSForCluster(
9991
ctx,
10092
&req.Cluster,
93+
defaultsNamespace,
10194
defaultInstallationConfigMapName,
10295
tigeraCM,
10396
); err != nil {
@@ -117,12 +110,13 @@ func (s crsStrategy) apply(
117110
func (s crsStrategy) ensureCNICRSForCluster(
118111
ctx context.Context,
119112
cluster *capiv1.Cluster,
113+
defaultsNamespace string,
120114
defaultInstallationConfigMapName string,
121115
tigeraConfigMap *corev1.ConfigMap,
122116
) error {
123117
defaultInstallationConfigMap := &corev1.ConfigMap{
124118
ObjectMeta: metav1.ObjectMeta{
125-
Namespace: s.config.defaultsNamespace,
119+
Namespace: defaultsNamespace,
126120
Name: defaultInstallationConfigMapName,
127121
},
128122
}
@@ -169,10 +163,11 @@ func (s crsStrategy) ensureCNICRSForCluster(
169163
func (s crsStrategy) ensureTigeraOperatorConfigMap(
170164
ctx context.Context,
171165
cluster *capiv1.Cluster,
166+
defaultsNamespace string,
172167
) (*corev1.ConfigMap, error) {
173168
defaultTigeraOperatorConfigMap := &corev1.ConfigMap{
174169
ObjectMeta: metav1.ObjectMeta{
175-
Namespace: s.config.defaultsNamespace,
170+
Namespace: defaultsNamespace,
176171
Name: s.config.defaultTigeraOperatorConfigMapName,
177172
},
178173
}

pkg/handlers/generic/lifecycle/cni/calico/strategy_helmaddon.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,10 @@ const (
2929
)
3030

3131
type helmAddonConfig struct {
32-
defaultsNamespace string
3332
defaultProviderInstallationValuesTemplatesConfigMapNames map[string]string
3433
}
3534

3635
func (c *helmAddonConfig) AddFlags(prefix string, flags *pflag.FlagSet) {
37-
flags.StringVar(
38-
&c.defaultsNamespace,
39-
prefix+".defaults-namespace",
40-
corev1.NamespaceDefault,
41-
"namespace of the default Helm values ConfigMaps",
42-
)
43-
4436
flags.StringToStringVar(
4537
&c.defaultProviderInstallationValuesTemplatesConfigMapNames,
4638
prefix+".default-provider-installation-values-templates-configmap-names",
@@ -61,6 +53,7 @@ type helmAddonStrategy struct {
6153
func (s helmAddonStrategy) apply(
6254
ctx context.Context,
6355
req *runtimehooksv1.AfterControlPlaneInitializedRequest,
56+
defaultsNamespace string,
6457
log logr.Logger,
6558
) error {
6659
infraKind := req.Cluster.Spec.InfrastructureRef.Kind
@@ -78,6 +71,7 @@ func (s helmAddonStrategy) apply(
7871
log.Info("Retrieving Calico installation values template for cluster")
7972
valuesTemplateConfigMap, err := s.retrieveValuesTemplateConfigMap(
8073
ctx,
74+
defaultsNamespace,
8175
defaultInstallationConfigMapName,
8276
)
8377
if err != nil {
@@ -125,11 +119,12 @@ func (s helmAddonStrategy) apply(
125119

126120
func (s helmAddonStrategy) retrieveValuesTemplateConfigMap(
127121
ctx context.Context,
122+
defaultsNamespace string,
128123
configMapName string,
129124
) (*corev1.ConfigMap, error) {
130125
configMap := &corev1.ConfigMap{
131126
ObjectMeta: metav1.ObjectMeta{
132-
Namespace: s.config.defaultsNamespace,
127+
Namespace: defaultsNamespace,
133128
Name: configMapName,
134129
},
135130
}

pkg/handlers/generic/lifecycle/cni/cilium/handler.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,21 @@ import (
1818
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/lifecycle"
1919
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/variables"
2020
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/clusterconfig"
21+
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/options"
2122
)
2223

2324
type addonStrategy interface {
24-
apply(context.Context, *runtimehooksv1.AfterControlPlaneInitializedRequest, logr.Logger) error
25+
apply(
26+
context.Context,
27+
*runtimehooksv1.AfterControlPlaneInitializedRequest,
28+
string,
29+
logr.Logger,
30+
) error
2531
}
2632

2733
type CNIConfig struct {
34+
*options.GlobalOptions
35+
2836
crsConfig crsConfig
2937
helmAddonConfig helmAddonConfig
3038
}
@@ -126,7 +134,7 @@ func (s *CiliumCNI) AfterControlPlaneInitialized(
126134
return
127135
}
128136

129-
if err := strategy.apply(ctx, req, log); err != nil {
137+
if err := strategy.apply(ctx, req, s.config.DefaultsNamespace(), log); err != nil {
130138
resp.SetStatus(runtimehooksv1.ResponseStatusFailure)
131139
resp.SetMessage(err.Error())
132140
return

pkg/handlers/generic/lifecycle/cni/cilium/strategy_crs.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,10 @@ import (
1919
)
2020

2121
type crsConfig struct {
22-
defaultsNamespace string
23-
2422
defaultCiliumConfigMapName string
2523
}
2624

2725
func (c *crsConfig) AddFlags(prefix string, flags *pflag.FlagSet) {
28-
flags.StringVar(
29-
&c.defaultsNamespace,
30-
prefix+".defaults-namespace",
31-
corev1.NamespaceDefault,
32-
"namespace of the ConfigMap used to deploy Cilium",
33-
)
34-
3526
flags.StringVar(
3627
&c.defaultCiliumConfigMapName,
3728
prefix+".default-cilium-configmap-name",
@@ -49,6 +40,7 @@ type crsStrategy struct {
4940
func (s crsStrategy) apply(
5041
ctx context.Context,
5142
req *runtimehooksv1.AfterControlPlaneInitializedRequest,
43+
defaultsNamespace string,
5244
log logr.Logger,
5345
) error {
5446
defaultCiliumConfigMap := &corev1.ConfigMap{
@@ -57,7 +49,7 @@ func (s crsStrategy) apply(
5749
Kind: "ConfigMap",
5850
},
5951
ObjectMeta: metav1.ObjectMeta{
60-
Namespace: s.config.defaultsNamespace,
52+
Namespace: defaultsNamespace,
6153
Name: s.config.defaultCiliumConfigMapName,
6254
},
6355
}

pkg/handlers/generic/lifecycle/cni/cilium/strategy_helmaddon.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,10 @@ const (
2929
)
3030

3131
type helmAddonConfig struct {
32-
defaultsNamespace string
3332
defaultValuesTemplateConfigMapName string
3433
}
3534

3635
func (c *helmAddonConfig) AddFlags(prefix string, flags *pflag.FlagSet) {
37-
flags.StringVar(
38-
&c.defaultsNamespace,
39-
prefix+".defaults-namespace",
40-
corev1.NamespaceDefault,
41-
"namespace of the default Helm values ConfigMap",
42-
)
43-
4436
flags.StringVar(
4537
&c.defaultValuesTemplateConfigMapName,
4638
prefix+".default-values-template-configmap-name",
@@ -58,10 +50,11 @@ type helmAddonStrategy struct {
5850
func (s helmAddonStrategy) apply(
5951
ctx context.Context,
6052
req *runtimehooksv1.AfterControlPlaneInitializedRequest,
53+
defaultsNamespace string,
6154
log logr.Logger,
6255
) error {
6356
log.Info("Retrieving Cilium installation values template for cluster")
64-
valuesTemplateConfigMap, err := s.retrieveValuesTemplateConfigMap(ctx)
57+
valuesTemplateConfigMap, err := s.retrieveValuesTemplateConfigMap(ctx, defaultsNamespace)
6558
if err != nil {
6659
return fmt.Errorf(
6760
"failed to retrieve Cilium installation values template ConfigMap for cluster: %w",
@@ -107,10 +100,11 @@ func (s helmAddonStrategy) apply(
107100

108101
func (s helmAddonStrategy) retrieveValuesTemplateConfigMap(
109102
ctx context.Context,
103+
defaultsNamespace string,
110104
) (*corev1.ConfigMap, error) {
111105
configMap := &corev1.ConfigMap{
112106
ObjectMeta: metav1.ObjectMeta{
113-
Namespace: s.config.defaultsNamespace,
107+
Namespace: defaultsNamespace,
114108
Name: s.config.defaultValuesTemplateConfigMapName,
115109
},
116110
}

pkg/handlers/generic/lifecycle/cpi/aws/handler.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,16 @@ import (
1616
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
1717

1818
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/k8s/client"
19+
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/options"
1920
)
2021

2122
type AWSCPIConfig struct {
22-
defaultsNamespace string
23+
*options.GlobalOptions
2324

2425
kubernetesMinorVersionToCPIConfigMapNames map[string]string
2526
}
2627

2728
func (a *AWSCPIConfig) AddFlags(prefix string, flags *pflag.FlagSet) {
28-
flags.StringVar(
29-
&a.defaultsNamespace,
30-
prefix+".defaults-namespace",
31-
corev1.NamespaceDefault,
32-
"namespace of the ConfigMap used to deploy AWS CPI",
33-
)
3429
flags.StringToStringVar(
3530
&a.kubernetesMinorVersionToCPIConfigMapNames,
3631
prefix+".default-aws-cpi-configmap-names",
@@ -74,7 +69,7 @@ func (a *AWSCPI) EnsureCPIConfigMapForCluster(
7469
configMapForMinorVersion := a.config.kubernetesMinorVersionToCPIConfigMapNames[minorVersion]
7570
cpiConfigMapForMinorVersion := &corev1.ConfigMap{
7671
ObjectMeta: metav1.ObjectMeta{
77-
Namespace: a.config.defaultsNamespace,
72+
Namespace: a.config.DefaultsNamespace(),
7873
Name: configMapForMinorVersion,
7974
},
8075
}

0 commit comments

Comments
 (0)