use DeploymentAvailability to check addon health (#212)

Signed-off-by: zhujian <jiazhu@redhat.com>
This commit is contained in:
Jian Zhu
2023-07-05 22:25:55 +08:00
committed by GitHub
parent d93268bb12
commit 21ee8d4c88
12 changed files with 246 additions and 49 deletions

View File

@@ -93,6 +93,7 @@ func (c *addonTemplateController) stopUnusedManagers(
stopFunc, ok := c.addonManagers[addOnName]
if ok {
stopFunc()
delete(c.addonManagers, addOnName)
klog.Infof("Stop the manager for addon %s", addOnName)
}
}

View File

@@ -7,10 +7,12 @@ import (
"time"
"github.com/openshift/library-go/pkg/operator/events/eventstesting"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/dynamic/dynamicinformer"
dynamicfake "k8s.io/client-go/dynamic/fake"
fakekube "k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/rest"
"open-cluster-management.io/addon-framework/pkg/addonmanager/addontesting"
"open-cluster-management.io/addon-framework/pkg/utils"
@@ -215,3 +217,52 @@ func TestReconcile(t *testing.T) {
}
}
}
func TestRunController(t *testing.T) {
cases := []struct {
name string
addonName string
expectedErr string
}{
{
name: "addon name empty",
addonName: "",
expectedErr: "addon name should be set",
},
{
name: "fake kubeconfig",
addonName: "test",
expectedErr: `Get "http://localhost/api": dial tcp [::1]:80: connect: connection refused`,
},
}
for _, c := range cases {
fakeAddonClient := fakeaddon.NewSimpleClientset()
addonInformers := addoninformers.NewSharedInformerFactory(fakeAddonClient, 10*time.Minute)
fakeDynamicClient := dynamicfake.NewSimpleDynamicClient(runtime.NewScheme())
dynamicInformerFactory := dynamicinformer.NewDynamicSharedInformerFactory(fakeDynamicClient, 0)
fakeClusterClient := fakecluster.NewSimpleClientset()
clusterInformers := clusterv1informers.NewSharedInformerFactory(fakeClusterClient, 10*time.Minute)
fakeWorkClient := fakework.NewSimpleClientset()
workInformers := workinformers.NewSharedInformerFactory(fakeWorkClient, 10*time.Minute)
hubKubeClient := fakekube.NewSimpleClientset()
controller := &addonTemplateController{
kubeConfig: &rest.Config{},
kubeClient: hubKubeClient,
addonClient: fakeAddonClient,
cmaLister: addonInformers.Addon().V1alpha1().ClusterManagementAddOns().Lister(),
addonManagers: make(map[string]context.CancelFunc),
addonInformers: addonInformers,
clusterInformers: clusterInformers,
dynamicInformers: dynamicInformerFactory,
workInformers: workInformers,
}
ctx := context.TODO()
err := controller.runController(ctx, c.addonName)
if len(c.expectedErr) == 0 {
assert.NoError(t, err)
}
assert.EqualErrorf(t, err, c.expectedErr, "name : %s, expected error %v, but got %v", c.name, c.expectedErr, err)
}
}

View File

@@ -4,7 +4,6 @@ import (
"fmt"
"github.com/valyala/fasttemplate"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -105,9 +104,11 @@ func (a *CRDTemplateAgentAddon) GetAgentAddonOptions() agent.AgentAddonOptions {
supportedConfigGVRs = append(supportedConfigGVRs, gvr)
}
return agent.AgentAddonOptions{
AddonName: a.addonName,
InstallStrategy: nil,
HealthProber: nil,
AddonName: a.addonName,
InstallStrategy: nil,
HealthProber: &agent.HealthProber{
Type: agent.HealthProberTypeDeploymentAvailability,
},
SupportedConfigGVRs: supportedConfigGVRs,
Registration: &agent.RegistrationOption{
CSRConfigurations: a.TemplateCSRConfigurationsFunc(),
@@ -159,7 +160,7 @@ func (a *CRDTemplateAgentAddon) decorateObjects(
newImageDecorator(privateValues),
}
for index, obj := range objects {
deployment, err := a.convertToDeployment(obj)
deployment, err := utils.ConvertToDeployment(obj)
if err != nil {
continue
}
@@ -176,26 +177,6 @@ func (a *CRDTemplateAgentAddon) decorateObjects(
return objects, nil
}
func (a *CRDTemplateAgentAddon) convertToDeployment(obj runtime.Object) (*appsv1.Deployment, error) {
if obj.GetObjectKind().GroupVersionKind().Group != "apps" ||
obj.GetObjectKind().GroupVersionKind().Kind != "Deployment" {
return nil, fmt.Errorf("not deployment object, %v", obj.GetObjectKind())
}
deployment := &appsv1.Deployment{}
uobj, ok := obj.(*unstructured.Unstructured)
if !ok {
return deployment, fmt.Errorf("not unstructured object, %v", obj.GetObjectKind())
}
err := runtime.DefaultUnstructuredConverter.
FromUnstructured(uobj.Object, deployment)
if err != nil {
return nil, err
}
return deployment, nil
}
// GetDesiredAddOnTemplateByAddon returns the desired template of the addon
func (a *CRDTemplateAgentAddon) GetDesiredAddOnTemplateByAddon(
addon *addonapiv1alpha1.ManagedClusterAddOn) (*addonapiv1alpha1.AddOnTemplate, error) {