Skip to content

Commit

Permalink
refactor setting the helm default variable of hostingClusterCapabilities
Browse files Browse the repository at this point in the history
Signed-off-by: zhujian <[email protected]>
  • Loading branch information
zhujian7 committed Mar 15, 2024
1 parent d22ba0c commit 3142006
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
6 changes: 6 additions & 0 deletions cmd/example/helloworld_hosted/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"open-cluster-management.io/addon-framework/pkg/addonmanager"
cmdfactory "open-cluster-management.io/addon-framework/pkg/cmd/factory"
"open-cluster-management.io/addon-framework/pkg/version"
clusterclientset "open-cluster-management.io/api/client/cluster/clientset/versioned"
)

func main() {
Expand Down Expand Up @@ -85,11 +86,16 @@ func runController(ctx context.Context, kubeConfig *rest.Config) error {
helloworld_hosted.AddonName,
utilrand.String(5))

clusterClient, err := clusterclientset.NewForConfig(kubeConfig)
if err != nil {
return err
}
agentAddon, err := addonfactory.NewAgentAddonFactory(
helloworld_hosted.AddonName, helloworld_hosted.FS, "manifests/templates").
WithGetValuesFuncs(helloworld.GetDefaultValues, addonfactory.GetValuesFromAddonAnnotation).
WithAgentRegistrationOption(registrationOption).
WithAgentHostedModeEnabledOption().
WithManagedClusterClient(clusterClient).
BuildTemplateAgentAddon()
if err != nil {
klog.Errorf("failed to build agent %v", err)
Expand Down
11 changes: 10 additions & 1 deletion pkg/addonfactory/addonfactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/klog/v2"
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
clusterclientset "open-cluster-management.io/api/client/cluster/clientset/versioned"
clusterv1 "open-cluster-management.io/api/cluster/v1"

"open-cluster-management.io/addon-framework/pkg/agent"
Expand All @@ -34,8 +35,10 @@ type AgentAddonFactory struct {
getValuesFuncs []GetValuesFunc
agentAddonOptions agent.AgentAddonOptions
// trimCRDDescription flag is used to trim the description of CRDs in manifestWork. disabled by default.
trimCRDDescription bool
trimCRDDescription bool
// Deprecated: use clusterClient to get the hosting cluster.
hostingCluster *clusterv1.ManagedCluster
clusterClient clusterclientset.Interface
agentInstallNamespace func(addon *addonapiv1alpha1.ManagedClusterAddOn) string
helmEngineStrict bool
}
Expand Down Expand Up @@ -131,11 +134,17 @@ func (f *AgentAddonFactory) WithConfigGVRs(gvrs ...schema.GroupVersionResource)

// WithHostingCluster defines the hosting cluster used in hosted mode. An AgentAddon may use this to provide
// additional metadata.
// Deprecated: use WithManagedClusterClient to set a cluster client that can get the hosting cluster.
func (f *AgentAddonFactory) WithHostingCluster(cluster *clusterv1.ManagedCluster) *AgentAddonFactory {
f.hostingCluster = cluster
return f
}

func (f *AgentAddonFactory) WithManagedClusterClient(c clusterclientset.Interface) *AgentAddonFactory {
f.clusterClient = c
return f
}

// WithAgentDeployTriggerClusterFilter defines the filter func to trigger the agent deploy/redploy when cluster info is
// changed. Addons that need information from the ManagedCluster resource when deploying the agent should use this
// function to set what information they need, otherwise the expected/up-to-date agent may be deployed delayed since the
Expand Down
20 changes: 16 additions & 4 deletions pkg/addonfactory/helm_agentaddon.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package addonfactory

import (
"bufio"
"context"
"fmt"
"io"
"sort"
Expand All @@ -12,11 +13,13 @@ import (
"helm.sh/helm/v3/pkg/engine"
"helm.sh/helm/v3/pkg/releaseutil"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/klog/v2"
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
clusterclientset "open-cluster-management.io/api/client/cluster/clientset/versioned"
clusterv1 "open-cluster-management.io/api/cluster/v1"

"open-cluster-management.io/addon-framework/pkg/addonmanager/constants"
Expand Down Expand Up @@ -50,6 +53,7 @@ type HelmAgentAddon struct {
agentAddonOptions agent.AgentAddonOptions
trimCRDDescription bool
hostingCluster *clusterv1.ManagedCluster
clusterClient clusterclientset.Interface
agentInstallNamespace func(addon *addonapiv1alpha1.ManagedClusterAddOn) string
helmEngineStrict bool
}
Expand All @@ -62,6 +66,7 @@ func newHelmAgentAddon(factory *AgentAddonFactory, chart *chart.Chart) *HelmAgen
agentAddonOptions: factory.agentAddonOptions,
trimCRDDescription: factory.trimCRDDescription,
hostingCluster: factory.hostingCluster,
clusterClient: factory.clusterClient,
agentInstallNamespace: factory.agentInstallNamespace,
helmEngineStrict: factory.helmEngineStrict,
}
Expand Down Expand Up @@ -240,10 +245,6 @@ func (a *HelmAgentAddon) getBuiltinValues(
return helmBuiltinValues, nil
}

func (a *HelmAgentAddon) SetHostingCluster(hostingCluster *clusterv1.ManagedCluster) {
a.hostingCluster = hostingCluster
}

func (a *HelmAgentAddon) getDefaultValues(
cluster *clusterv1.ManagedCluster,
addon *addonapiv1alpha1.ManagedClusterAddOn) (Values, error) {
Expand All @@ -258,6 +259,17 @@ func (a *HelmAgentAddon) getDefaultValues(

if a.hostingCluster != nil {
defaultValues.HostingClusterCapabilities = *a.capabilities(a.hostingCluster, addon)
} else if a.clusterClient != nil {
hostingClusterName := addon.GetAnnotations()[addonapiv1alpha1.HostingClusterNameAnnotationKey]
if len(hostingClusterName) > 0 {
hostingCluster, err := a.clusterClient.ClusterV1().ManagedClusters().
Get(context.TODO(), hostingClusterName, metav1.GetOptions{})
if err != nil {
klog.Errorf("failed to get hostingCluster %s. err:%v", hostingClusterName, err)
return nil, err
}
defaultValues.HostingClusterCapabilities = *a.capabilities(hostingCluster, addon)
}
}

helmDefaultValues, err := JsonStructToValues(defaultValues)
Expand Down
30 changes: 26 additions & 4 deletions pkg/addonfactory/helm_agentaddon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"k8s.io/client-go/kubernetes/scheme"
"open-cluster-management.io/addon-framework/pkg/agent"
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
clusterclientset "open-cluster-management.io/api/client/cluster/clientset/versioned/fake"
clusterv1 "open-cluster-management.io/api/cluster/v1"
clusterv1apha1 "open-cluster-management.io/api/cluster/v1alpha1"
)
Expand Down Expand Up @@ -66,6 +67,7 @@ func TestChartAgentAddon_Manifests(t *testing.T) {
scheme *runtime.Scheme
clusterName string
hostingCluster *clusterv1.ManagedCluster
getHostingClusterWithClient bool
addonName string
installNamespace string
annotationValues string
Expand Down Expand Up @@ -146,6 +148,19 @@ func TestChartAgentAddon_Manifests(t *testing.T) {
expectedObjCnt: 5,
expectedNamespace: true,
},
{
name: "template render ok, getting hosting cluster with client",
scheme: testScheme,
clusterName: "cluster1",
hostingCluster: NewFakeManagedCluster("hosting-cluster", "1.25.0"),
getHostingClusterWithClient: true,
addonName: "helloworld",
installNamespace: "myNs",
expectedInstallNamespace: "myNs",
expectedImage: "quay.io/testimage:test",
expectedObjCnt: 5,
expectedNamespace: true,
},
}

for _, c := range cases {
Expand All @@ -166,13 +181,20 @@ func TestChartAgentAddon_Manifests(t *testing.T) {
cluster := NewFakeManagedCluster(c.clusterName, "1.10.1")
clusterAddon := NewFakeManagedClusterAddon(c.addonName, c.clusterName, c.installNamespace, c.annotationValues)

agentAddon, err := NewAgentAddonFactory(c.addonName, chartFS, "testmanifests/chart").
factory := NewAgentAddonFactory(c.addonName, chartFS, "testmanifests/chart").
WithGetValuesFuncs(getValuesFuncs...).
WithScheme(c.scheme).
WithTrimCRDDescription().
WithAgentRegistrationOption(&agent.RegistrationOption{}).
WithHostingCluster(c.hostingCluster).
BuildHelmAgentAddon()
WithAgentRegistrationOption(&agent.RegistrationOption{})
if c.getHostingClusterWithClient && c.hostingCluster != nil {
clusterClient := clusterclientset.NewSimpleClientset(c.hostingCluster)
factory = factory.WithManagedClusterClient(clusterClient)
clusterAddon.Annotations[addonapiv1alpha1.HostingClusterNameAnnotationKey] = c.hostingCluster.Name
}
if !c.getHostingClusterWithClient && c.hostingCluster != nil {
factory = factory.WithHostingCluster(c.hostingCluster)
}
agentAddon, err := factory.BuildHelmAgentAddon()
if err != nil {
t.Errorf("expected no error, got err %v", err)
}
Expand Down

0 comments on commit 3142006

Please sign in to comment.