Skip to content

Commit

Permalink
add createNamespace option to chart render
Browse files Browse the repository at this point in the history
Signed-off-by: Zhiwei Yin <zyin@redhat.com>
  • Loading branch information
zhiweiyin318 committed Aug 27, 2024
1 parent 9cedd46 commit d4ac71a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 6 deletions.
2 changes: 2 additions & 0 deletions deploy/cluster-manager/chart/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ var ChartFiles embed.FS
const ChartName = "cluster-manager"

type ChartConfig struct {
// CreateNamespace is used in the render function to append the release ns in the objects.
CreateNamespace bool `json:"createNamespace,omitempty"`
// ReplicaCount is the replicas for the clusterManager operator deployment.
ReplicaCount int `json:"replicaCount,omitempty"`
// Images is the configurations for all images used in operator deployment and clusterManager CR.
Expand Down
2 changes: 2 additions & 0 deletions deploy/klusterlet/chart/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ var ChartFiles embed.FS
const ChartName = "klusterlet"

type ChartConfig struct {
// CreateNamespace is used in the render function to append the release ns in the objects.
CreateNamespace bool `json:"createNamespace,omitempty"`
// ReplicaCount is the replicas for the klusterlet operator deployment.
ReplicaCount int `json:"replicaCount,omitempty"`
// Images is the configurations for all images used in operator deployment and klusterlet CR.
Expand Down
50 changes: 46 additions & 4 deletions pkg/operator/helpers/chart/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/engine"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/json"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/klog/v2"
"sigs.k8s.io/yaml"

clustermanagerchart "open-cluster-management.io/ocm/deploy/cluster-manager/chart"
klusterletchart "open-cluster-management.io/ocm/deploy/klusterlet/chart"
Expand All @@ -33,8 +35,24 @@ func NewDefaultKlusterletChartConfig() *klusterletchart.ChartConfig {
}
}

func RenderChart[T *clustermanagerchart.ChartConfig | *klusterletchart.ChartConfig](config T,
namespace string, chartName string, fs embed.FS) ([][]byte, error) {
func RenderClusterManagerChart(config *clustermanagerchart.ChartConfig, namespace string) ([][]byte, error) {
if namespace == "" {
return nil, fmt.Errorf("cluster manager chart namespace is required")
}
return renderChart(config, namespace, config.CreateNamespace,
clustermanagerchart.ChartName, clustermanagerchart.ChartFiles)
}

func RenderKlusterletChart(config *klusterletchart.ChartConfig, namespace string) ([][]byte, error) {
if namespace == "" {
return nil, fmt.Errorf("klusterlet chart namespace is required")
}
return renderChart(config, namespace, config.CreateNamespace,
klusterletchart.ChartName, klusterletchart.ChartFiles)
}

func renderChart[T *clustermanagerchart.ChartConfig | *klusterletchart.ChartConfig](config T,
namespace string, createNamespace bool, chartName string, fs embed.FS) ([][]byte, error) {
// chartName is the prefix of chart path here
operatorChart, err := LoadChart(fs, chartName)
if err != nil {
Expand Down Expand Up @@ -63,7 +81,17 @@ func RenderChart[T *clustermanagerchart.ChartConfig | *klusterletchart.ChartConf
return nil, fmt.Errorf("error rendering cluster manager chart: %v", err)
}

return rawObjects, nil
rstObjects := [][]byte{}
if createNamespace {
nsObj, err := newNamespaceRawObject(namespace)
if err != nil {
return nil, err
}
rstObjects = [][]byte{nsObj}
}
rstObjects = append(rstObjects, rawObjects...)

return rstObjects, nil
}

func getFiles(manifestFS embed.FS) ([]string, error) {
Expand Down Expand Up @@ -172,3 +200,17 @@ func renderManifests(chart *chart.Chart, values chartutil.Values) ([][]byte, err
}
return rawObjects, nil
}

func newNamespaceRawObject(namespace string) ([]byte, error) {
ns := &corev1.Namespace{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Namespace",
},
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
},
}

return yaml.Marshal(ns)
}
35 changes: 33 additions & 2 deletions pkg/operator/helpers/chart/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ func TestClusterManagerConfig(t *testing.T) {
},
expectedObjCnt: 7,
},
{
name: "create namespace",
namespace: "multicluster-engine",
chartConfig: func() *clustermanagerchart.ChartConfig {
config := NewDefaultClusterManagerChartConfig()
config.CreateBootstrapToken = true
config.CreateNamespace = true
return config
},
expectedObjCnt: 10,
},
}

for _, c := range cases {
Expand All @@ -99,7 +110,7 @@ func TestClusterManagerConfig(t *testing.T) {
version = config.Images.Tag
}

objects, err := RenderChart(config, c.namespace, clustermanagerchart.ChartName, clustermanagerchart.ChartFiles)
objects, err := RenderClusterManagerChart(config, c.namespace)
if err != nil {
t.Errorf("error rendering chart: %v", err)
}
Expand All @@ -115,6 +126,10 @@ func TestClusterManagerConfig(t *testing.T) {
}
outputObjs = append(outputObjs, obj)
switch object := obj.(type) {
case *corev1.Namespace:
if object.Name != c.namespace {
t.Errorf("expected namespace %s, got %s", c.namespace, object.Name)
}
case *appsv1.Deployment:
if object.Namespace != c.namespace {
t.Errorf("expected namespace is %s, but got %s", c.namespace, object.Namespace)
Expand Down Expand Up @@ -213,6 +228,18 @@ func TestKlusterletConfig(t *testing.T) {
},
expectedObjCnt: 2,
},
{
name: "create namespace",
namespace: "open-cluster-management",
chartConfig: func() *klusterletchart.ChartConfig {
config := NewDefaultKlusterletChartConfig()
config.Klusterlet.ClusterName = "testCluster"
config.Klusterlet.Mode = operatorv1.InstallModeSingleton
config.CreateNamespace = true
return config
},
expectedObjCnt: 7,
},
}

for _, c := range cases {
Expand All @@ -227,7 +254,7 @@ func TestKlusterletConfig(t *testing.T) {
version = config.Images.Tag
}

objects, err := RenderChart(config, c.namespace, klusterletchart.ChartName, klusterletchart.ChartFiles)
objects, err := RenderKlusterletChart(config, c.namespace)
if err != nil {
t.Errorf("error rendering chart: %v", err)
}
Expand All @@ -243,6 +270,10 @@ func TestKlusterletConfig(t *testing.T) {
}
outputObjs = append(outputObjs, obj)
switch object := obj.(type) {
case *corev1.Namespace:
if object.Name != c.namespace {
t.Errorf("expected namespace %s, got %s", c.namespace, object.Name)
}
case *appsv1.Deployment:
if object.Namespace != c.namespace {
t.Errorf("expected namespace is %s, but got %s", c.namespace, object.Namespace)
Expand Down

0 comments on commit d4ac71a

Please sign in to comment.