Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 read config referent from the desired config #269

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 1 addition & 36 deletions examples/helloworld/helloworld.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"open-cluster-management.io/addon-framework/pkg/utils"
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
clusterv1 "open-cluster-management.io/api/cluster/v1"
workapiv1 "open-cluster-management.io/api/work/v1"
)

const (
Expand Down Expand Up @@ -56,40 +55,6 @@ func GetDefaultValues(cluster *clusterv1.ManagedCluster,

func AgentHealthProber() *agent.HealthProber {
return &agent.HealthProber{
Type: agent.HealthProberTypeWork,
WorkProber: &agent.WorkHealthProber{
ProbeFields: []agent.ProbeField{
{
ResourceIdentifier: workapiv1.ResourceIdentifier{
Group: "apps",
Resource: "deployments",
Name: "helloworld-agent",
Namespace: InstallationNamespace,
},
ProbeRules: []workapiv1.FeedbackRule{
{
Type: workapiv1.WellKnownStatusType,
},
},
},
},
HealthCheck: func(identifier workapiv1.ResourceIdentifier, result workapiv1.StatusFeedbackResult) error {
if len(result.Values) == 0 {
return fmt.Errorf("no values are probed for deployment %s/%s", identifier.Namespace, identifier.Name)
}
for _, value := range result.Values {
if value.Name != "ReadyReplicas" {
continue
}

if *value.Value.Integer >= 1 {
return nil
}

return fmt.Errorf("readyReplica is %d for deployement %s/%s", *value.Value.Integer, identifier.Namespace, identifier.Name)
}
return fmt.Errorf("readyReplica is not probed")
},
},
Type: agent.HealthProberTypeDeploymentAvailability,
}
}
8 changes: 7 additions & 1 deletion examples/helloworld_helm/helloworld_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ func GetImageValues(kubeClient kubernetes.Interface) addonfactory.GetValuesFunc
continue
}

configMap, err := kubeClient.CoreV1().ConfigMaps(config.Namespace).Get(context.Background(), config.Name, metav1.GetOptions{})
if config.DesiredConfig == nil {
continue
}

configMap, err := kubeClient.CoreV1().
ConfigMaps(config.DesiredConfig.Namespace).
Get(context.Background(), config.DesiredConfig.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
Expand Down
111 changes: 16 additions & 95 deletions pkg/addonfactory/addondeploymentconfig.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package addonfactory

import (
"context"
"encoding/json"
"fmt"
"strings"
Expand All @@ -23,74 +22,6 @@ var AddOnDeploymentConfigGVR = schema.GroupVersionResource{
Resource: "addondeploymentconfigs",
}

// AddOnDeloymentConfigToValuesFunc transform the AddOnDeploymentConfig object into Values object
// The transformation logic depends on the definition of the addon template
// Deprecated: use AddOnDeploymentConfigToValuesFunc instead.
type AddOnDeloymentConfigToValuesFunc func(config addonapiv1alpha1.AddOnDeploymentConfig) (Values, error)

// NewAddOnDeloymentConfigGetter returns a AddOnDeloymentConfigGetter with addon client
// Deprecated: use NewAddOnDeploymentConfigGetter in pkg/utils package instead.
func NewAddOnDeloymentConfigGetter(addonClient addonv1alpha1client.Interface) utils.AddOnDeploymentConfigGetter {
return utils.NewAddOnDeploymentConfigGetter(addonClient)
}

// GetAddOnDeloymentConfigValues uses AddOnDeloymentConfigGetter to get the AddOnDeploymentConfig object, then
// uses AddOnDeloymentConfigToValuesFunc to transform the AddOnDeploymentConfig object to Values object
// If there are multiple AddOnDeploymentConfig objects in the AddOn ConfigReferences, the big index object will
// override the one from small index
// Deprecated: use GetAddOnDeploymentConfigValues instead.
func GetAddOnDeloymentConfigValues(
getter utils.AddOnDeploymentConfigGetter, toValuesFuncs ...AddOnDeloymentConfigToValuesFunc) GetValuesFunc {
return func(cluster *clusterv1.ManagedCluster, addon *addonapiv1alpha1.ManagedClusterAddOn) (Values, error) {
var lastValues = Values{}
for _, config := range addon.Status.ConfigReferences {
if config.ConfigGroupResource.Group != utils.AddOnDeploymentConfigGVR.Group ||
config.ConfigGroupResource.Resource != utils.AddOnDeploymentConfigGVR.Resource {
continue
}

addOnDeploymentConfig, err := getter.Get(context.Background(), config.Namespace, config.Name)
if err != nil {
return nil, err
}

for _, toValuesFunc := range toValuesFuncs {
values, err := toValuesFunc(*addOnDeploymentConfig)
if err != nil {
return nil, err
}
lastValues = MergeValues(lastValues, values)
}
}

return lastValues, nil
}
}

// ToAddOnDeloymentConfigValues transform the AddOnDeploymentConfig object into Values object that is a plain value map
// for example: the spec of one AddOnDeploymentConfig is:
//
// {
// customizedVariables: [{name: "Image", value: "img"}, {name: "ImagePullPolicy", value: "Always"}],
// nodePlacement: {nodeSelector: {"host": "ssd"}, tolerations: {"key": "test"}},
// }
//
// after transformed, the key set of Values object will be: {"Image", "ImagePullPolicy", "NodeSelector", "Tolerations"}
// Deprecated: use ToAddOnDeploymentConfigValues instead.
func ToAddOnDeloymentConfigValues(config addonapiv1alpha1.AddOnDeploymentConfig) (Values, error) {
values, err := ToAddOnCustomizedVariableValues(config)
if err != nil {
return nil, err
}

if config.Spec.NodePlacement != nil {
values["NodeSelector"] = config.Spec.NodePlacement.NodeSelector
values["Tolerations"] = config.Spec.NodePlacement.Tolerations
}

return values, nil
}

// ToAddOnNodePlacementValues only transform the AddOnDeploymentConfig NodePlacement part into Values object that has
// a specific for helm chart values
// for example: the spec of one AddOnDeploymentConfig is:
Expand Down Expand Up @@ -214,24 +145,21 @@ func GetAddOnDeploymentConfigValues(
getter utils.AddOnDeploymentConfigGetter, toValuesFuncs ...AddOnDeploymentConfigToValuesFunc) GetValuesFunc {
return func(cluster *clusterv1.ManagedCluster, addon *addonapiv1alpha1.ManagedClusterAddOn) (Values, error) {
var lastValues = Values{}
for _, config := range addon.Status.ConfigReferences {
if config.ConfigGroupResource.Group != utils.AddOnDeploymentConfigGVR.Group ||
config.ConfigGroupResource.Resource != utils.AddOnDeploymentConfigGVR.Resource {
continue
}
addOnDeploymentConfig, err := utils.GetDesiredAddOnDeploymentConfig(addon, getter)
if err != nil {
return lastValues, err
}

if addOnDeploymentConfig == nil {
return lastValues, nil
}

addOnDeploymentConfig, err := getter.Get(context.Background(), config.Namespace, config.Name)
for _, toValuesFunc := range toValuesFuncs {
values, err := toValuesFunc(*addOnDeploymentConfig)
if err != nil {
return nil, err
}

for _, toValuesFunc := range toValuesFuncs {
values, err := toValuesFunc(*addOnDeploymentConfig)
if err != nil {
return nil, err
}
lastValues = MergeValues(lastValues, values)
}
lastValues = MergeValues(lastValues, values)
}

return lastValues, nil
Expand Down Expand Up @@ -349,19 +277,12 @@ func getRegistriesFromClusterAnnotation(
// - Image registries configured in the addonDeploymentConfig will take precedence over the managed cluster annotation
func GetAgentImageValues(getter utils.AddOnDeploymentConfigGetter, imageKey, image string) GetValuesFunc {
return func(cluster *clusterv1.ManagedCluster, addon *addonapiv1alpha1.ManagedClusterAddOn) (Values, error) {

addOnDeploymentConfig, err := utils.GetDesiredAddOnDeploymentConfig(addon, getter)
if err != nil {
return nil, err
}
// Get image from AddOnDeploymentConfig
for _, config := range addon.Status.ConfigReferences {
if config.ConfigGroupResource.Group != utils.AddOnDeploymentConfigGVR.Group ||
config.ConfigGroupResource.Resource != utils.AddOnDeploymentConfigGVR.Resource {
continue
}

addOnDeploymentConfig, err := getter.Get(context.Background(), config.Namespace, config.Name)
if err != nil {
return nil, err
}

if addOnDeploymentConfig != nil {
values, overrode, err := overrideImageWithKeyValue(imageKey, image,
getRegistriesFromAddonDeploymentConfig(*addOnDeploymentConfig))
if err != nil {
Expand Down
Loading
Loading