Skip to content

Commit

Permalink
Merge branch 'bfs/mgianata-vz-11307' of https://github.com/verrazzano…
Browse files Browse the repository at this point in the history
…/rancher into bfs/mgianata-vz-11307
  • Loading branch information
mgianatagh committed Oct 22, 2023
2 parents d8e1051 + b956e3c commit e074bb8
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 38 deletions.
138 changes: 105 additions & 33 deletions pkg/controllers/dashboard/systemcharts/controller.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package systemcharts handles the reconciliation of systemcharts installed by rancher in the rancher-charts repo.
package systemcharts

import (
Expand All @@ -8,23 +9,37 @@ import (
v3 "github.com/rancher/rancher/pkg/apis/management.cattle.io/v3"
"github.com/rancher/rancher/pkg/controllers/dashboard/chart"
"github.com/rancher/rancher/pkg/features"
namespace "github.com/rancher/rancher/pkg/namespace"
v1 "github.com/rancher/rancher/pkg/generated/norman/core/v1"
"github.com/rancher/rancher/pkg/namespace"
"github.com/rancher/rancher/pkg/settings"
"github.com/rancher/rancher/pkg/wrangler"
"github.com/rancher/wrangler/pkg/data"
corecontrollers "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
"github.com/rancher/wrangler/pkg/relatedresource"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/errors"
apierror "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
)

const (
repoName = "rancher-charts"
webhookChartName = "rancher-webhook"
webhookImage = "rancher/rancher-webhook"
priorityClassKey = "priorityClassName"
)

var (
primaryImages = map[string]string{
chart.WebhookChartName: "rancher/rancher-webhook",
chart.ProvisioningCAPIChartName: "rancher/mirrored-cluster-api-controller",
}
watchedSettings = map[string]struct{}{
settings.RancherWebhookMinVersion.Name: {},
settings.RancherWebhookVersion.Name: {},
settings.SystemDefaultRegistry.Name: {},
settings.ShellImage.Name: {},
}
)

// Register is called to create a new handler and subscribe to change events.
func Register(ctx context.Context, wContext *wrangler.Context, registryOverride string) error {
h := &handler{
manager: wContext.SystemChartsManager,
Expand All @@ -34,20 +49,12 @@ func Register(ctx context.Context, wContext *wrangler.Context, registryOverride
}

wContext.Catalog.ClusterRepo().OnChange(ctx, "bootstrap-charts", h.onRepo)
relatedresource.WatchClusterScoped(ctx, "bootstrap-charts", func(namespace, name string, obj runtime.Object) ([]relatedresource.Key, error) {
return []relatedresource.Key{{
Name: repoName,
}}, nil
}, wContext.Catalog.ClusterRepo(), wContext.Mgmt.Feature())
relatedresource.WatchClusterScoped(ctx, "bootstrap-charts", relatedFeatures, wContext.Catalog.ClusterRepo(), wContext.Mgmt.Feature())

relatedresource.WatchClusterScoped(ctx, "bootstrap-settings-charts", func(namespace, name string, obj runtime.Object) ([]relatedresource.Key, error) {
if s, ok := obj.(*v3.Setting); ok && (s.Name == "rancher-webhook-version" || s.Name == "rancher-webhook-min-version") {
return []relatedresource.Key{{
Name: repoName,
}}, nil
}
return nil, nil
}, wContext.Catalog.ClusterRepo(), wContext.Mgmt.Setting())
relatedresource.WatchClusterScoped(ctx, "bootstrap-settings-charts", relatedSettings, wContext.Catalog.ClusterRepo(), wContext.Mgmt.Setting())

// ensure the system charts are installed with the correct values when there are changes to the rancher config map
relatedresource.WatchClusterScoped(ctx, "bootstrap-configmap-charts", relatedConfigMaps, wContext.Catalog.ClusterRepo(), wContext.Core.ConfigMap())
return nil
}

Expand Down Expand Up @@ -101,7 +108,9 @@ func (h *handler) onRepo(key string, repo *catalog.ClusterRepo) (*catalog.Cluste
if !ok {
imageSettings = map[string]interface{}{}
}
imageSettings["repository"] = h.registryOverride + "/" + webhookImage
if image, ok := primaryImages[chartDef.ChartName]; ok {
imageSettings["repository"] = h.registryOverride + "/" + image
}
values["image"] = imageSettings
installImageOverride = h.registryOverride + "/" + settings.ShellImage.Get()
}
Expand All @@ -111,17 +120,18 @@ func (h *handler) onRepo(key string, repo *catalog.ClusterRepo) (*catalog.Cluste
}
}

if chartDef.ChartName == webhookChartName {
if chartDef.ChartName == chart.WebhookChartName {
overrideRancherWebhookImage(values)
}
// webhook needs to be able to adopt the MutatingWebhookConfiguration which originally wasn't a part of the
// chart definition, but is now part of the chart definition
minVersion := chartDef.MinVersionSetting.Get()
exactVersion := chartDef.ExactVersionSetting.Get()
if chartDef.ChartName == webhookChartName && minVersion != "" {
if chartDef.ChartName == chart.WebhookChartName && minVersion != "" {
exactVersion = ""
}
if err := h.manager.Ensure(chartDef.ReleaseNamespace, chartDef.ChartName, minVersion, exactVersion, values, chartDef.ChartName == webhookChartName, installImageOverride); err != nil {
forceAdopt := chartDef.ChartName == chart.WebhookChartName || chartDef.ChartName == chart.ProvisioningCAPIChartName
if err := h.manager.Ensure(chartDef.ReleaseNamespace, chartDef.ChartName, minVersion, exactVersion, values, forceAdopt, installImageOverride); err != nil {
return repo, err
}
}
Expand All @@ -133,39 +143,101 @@ func (h *handler) getChartsToInstall() []*chart.Definition {
return []*chart.Definition{
{
ReleaseNamespace: namespace.System,
ChartName: webhookChartName,
ChartName: chart.WebhookChartName,
MinVersionSetting: settings.RancherWebhookMinVersion,
ExactVersionSetting: settings.RancherWebhookVersion,
Values: func() map[string]interface{} {
values := map[string]interface{}{
"capi": map[string]interface{}{
"enabled": features.EmbeddedClusterAPI.Enabled(),
"enabled": false,
},
"mcm": map[string]interface{}{
"enabled": features.MCM.Enabled(),
"enabled": func() bool {
return features.MCM.Enabled() || features.EmbeddedClusterAPI.Enabled()
},
},
}
// add priority class value.
if priorityClassName, err := h.chartsConfig.GetPriorityClassName(); err != nil {
if !apierror.IsNotFound(err) {
logrus.Warnf("Failed to get rancher priorityClassName for 'rancher-webhook': %v", err)
// add priority class value
if priorityClassName, err := h.chartsConfig.GetGlobalValue(chart.PriorityClassKey); err != nil {
if !chart.IsNotFoundError(err) {
logrus.Warnf("Failed to get rancher %s for 'rancher-webhook': %s", chart.PriorityClassKey, err.Error())
}
} else {
values[chart.PriorityClassKey] = priorityClassName
values[priorityClassKey] = priorityClassName
}
return values
},
Enabled: func() bool {
return features.MCM.Enabled() || features.EmbeddedClusterAPI.Enabled()

// get custom values for the rancher-webhook
configMapValues, err := h.chartsConfig.GetChartValues(chart.WebhookChartName)
if err != nil && !chart.IsNotFoundError(err) {
logrus.Warnf("Failed to get rancher rancherWebhookValues %s", err.Error())
}

return data.MergeMaps(values, configMapValues)
},
Enabled: func() bool { return true },
},
{
ReleaseNamespace: "rancher-operator-system",
ChartName: "rancher-operator",
Uninstall: true,
RemoveNamespace: true,
},
{
ReleaseNamespace: namespace.ProvisioningCAPINamespace,
ChartName: chart.ProvisioningCAPIChartName,
Values: func() map[string]interface{} {
values := map[string]interface{}{}
// add priority class value
if priorityClassName, err := h.chartsConfig.GetGlobalValue(chart.PriorityClassKey); err != nil {
if !chart.IsNotFoundError(err) {
logrus.Warnf("Failed to get rancher %s for 'rancher-provisioning-capi': %s", chart.PriorityClassKey, err.Error())
}
} else {
values[priorityClassKey] = priorityClassName
}

// get custom values for the rancher-provisioning-capi
configMapValues, err := h.chartsConfig.GetChartValues(chart.ProvisioningCAPIChartName)
if err != nil && !chart.IsNotFoundError(err) {
logrus.Warnf("Failed to get rancher-provisioning-capi %s", err.Error())
}

return data.MergeMaps(values, configMapValues)
},
Enabled: func() bool { return true },
Uninstall: !features.EmbeddedClusterAPI.Enabled(),
RemoveNamespace: !features.EmbeddedClusterAPI.Enabled(),
},
}
}

func relatedFeatures(_, _ string, obj runtime.Object) ([]relatedresource.Key, error) {
if _, ok := obj.(*v3.Feature); ok {
return []relatedresource.Key{{
Name: repoName,
}}, nil
}
return nil, nil
}

func relatedSettings(_, _ string, obj runtime.Object) ([]relatedresource.Key, error) {
if f, ok := obj.(*v3.Setting); ok {
if _, ok := watchedSettings[f.Name]; ok {
return []relatedresource.Key{{
Name: repoName,
}}, nil
}
}
return nil, nil
}

func relatedConfigMaps(_, _ string, obj runtime.Object) ([]relatedresource.Key, error) {
if configMap, ok := obj.(*v1.ConfigMap); ok && configMap.Namespace == namespace.System && (configMap.Name == chart.CustomValueMapName || configMap.Name == settings.ConfigMapName.Get()) {
return []relatedresource.Key{{
Name: repoName,
}}, nil
}
return nil, nil
}

// overrideRancherWebhookImage sets Helm chart values to override the Rancher Webhook image name and/or tag based on environment variables.
Expand Down
3 changes: 0 additions & 3 deletions pkg/crds/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ func RequiredCRDs() []string {
requiredCRDS := BasicCRDs()
if features.ProvisioningV2.Enabled() {
requiredCRDS = append(requiredCRDS, ProvisioningV2CRDs()...)
if features.RKE2.Enabled() {
requiredCRDS = append(requiredCRDS, RKE2CRDs()...)
}
if features.EmbeddedClusterAPI.Enabled() {
requiredCRDS = append(requiredCRDS, CAPICRDs()...)
}
Expand Down
2 changes: 0 additions & 2 deletions pkg/image/origins.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ var OriginMap = map[string]string{
"mirrored-cilium-cilium": "https://github.com/cilium/cilium",
"mirrored-cilium-cilium-envoy": "https://github.com/cilium/proxy",
"mirrored-cilium-cilium-etcd-operator": "https://github.com/cilium/cilium-etcd-operator",
"mirrored-cilium-cilium-envoy": "https://github.com/cilium/cilium-envoy",
"mirrored-cilium-kvstoremesh": "https://github.com/cilium/kvstoremesh",
"mirrored-cilium-clustermesh-apiserver": "https://github.com/cilium/clustermesh-apiserver",
"mirrored-cilium-hubble-relay": "https://github.com/cilium/hubble",
"mirrored-cilium-hubble-ui": "https://github.com/cilium/hubble-ui",
Expand Down

0 comments on commit e074bb8

Please sign in to comment.