-
Notifications
You must be signed in to change notification settings - Fork 4
/
strategy_helmaddon.go
108 lines (93 loc) · 3.14 KB
/
strategy_helmaddon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2023 Nutanix. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package nfd
import (
"context"
"fmt"
"github.com/go-logr/logr"
"github.com/spf13/pflag"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
caaphv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-addon-provider-helm/api/v1alpha1"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/k8s/client"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/config"
lifecycleutils "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/utils"
)
const (
defaultHelmReleaseName = "node-feature-discovery"
defaultHelmReleaseNamespace = "node-feature-discovery"
)
type helmAddonConfig struct {
defaultValuesTemplateConfigMapName string
}
func (c *helmAddonConfig) AddFlags(prefix string, flags *pflag.FlagSet) {
flags.StringVar(
&c.defaultValuesTemplateConfigMapName,
prefix+".default-values-template-configmap-name",
"default-nfd-helm-values-template",
"default values ConfigMap name",
)
}
type helmAddonStrategy struct {
config helmAddonConfig
client ctrlclient.Client
helmChart *config.HelmChart
}
func (s helmAddonStrategy) apply(
ctx context.Context,
req *runtimehooksv1.AfterControlPlaneInitializedRequest,
defaultsNamespace string,
log logr.Logger,
) error {
log.Info("Retrieving NFD installation values template for cluster")
values, err := lifecycleutils.RetrieveValuesTemplate(
ctx,
s.client,
s.config.defaultValuesTemplateConfigMapName,
defaultsNamespace,
)
if err != nil {
return fmt.Errorf(
"failed to retrieve NFD installation values template for cluster: %w",
err,
)
}
values += fmt.Sprintf(`
image:
tag: v%s-minimal
`, s.helmChart.Version)
hcp := &caaphv1.HelmChartProxy{
TypeMeta: metav1.TypeMeta{
APIVersion: caaphv1.GroupVersion.String(),
Kind: "HelmChartProxy",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: req.Cluster.Namespace,
Name: "node-feature-discovery-" + req.Cluster.Name,
},
Spec: caaphv1.HelmChartProxySpec{
RepoURL: s.helmChart.Repository,
ChartName: s.helmChart.Name,
ClusterSelector: metav1.LabelSelector{
MatchLabels: map[string]string{clusterv1.ClusterNameLabel: req.Cluster.Name},
},
ReleaseNamespace: defaultHelmReleaseNamespace,
ReleaseName: defaultHelmReleaseName,
Version: s.helmChart.Version,
ValuesTemplate: values,
},
}
if err := controllerutil.SetOwnerReference(&req.Cluster, hcp, s.client.Scheme()); err != nil {
return fmt.Errorf(
"failed to set owner reference on NFD installation HelmChartProxy: %w",
err,
)
}
if err := client.ServerSideApply(ctx, s.client, hcp, client.ForceOwnership); err != nil {
return fmt.Errorf("failed to apply NFD installation HelmChartProxy: %w", err)
}
return nil
}