Removes dependency on a versioned ARO client

Operator is not longer dependant on a version client for ARO objects
and is now using a split client.
This commit is contained in:
Mikalai Radchuk 2023-01-11 15:52:48 +00:00 коммит произвёл Mikalai Radchuk
Родитель 8080049aa6
Коммит ec7cb16295
14 изменённых файлов: 73 добавлений и 95 удалений

Просмотреть файл

@ -22,7 +22,6 @@ import (
"github.com/Azure/ARO-RP/pkg/env"
pkgoperator "github.com/Azure/ARO-RP/pkg/operator"
aroclient "github.com/Azure/ARO-RP/pkg/operator/clientset/versioned"
"github.com/Azure/ARO-RP/pkg/operator/controllers/alertwebhook"
"github.com/Azure/ARO-RP/pkg/operator/controllers/autosizednodes"
"github.com/Azure/ARO-RP/pkg/operator/controllers/banner"
@ -81,10 +80,6 @@ func operator(ctx context.Context, log *logrus.Entry) error {
return err
}
arocli, err := aroclient.NewForConfig(restConfig)
if err != nil {
return err
}
configcli, err := configclient.NewForConfig(restConfig)
if err != nil {
return err
@ -188,7 +183,7 @@ func operator(ctx context.Context, log *logrus.Entry) error {
}
if err = (machine.NewReconciler(
log.WithField("controller", machine.ControllerName),
arocli, maocli, isLocalDevelopmentMode, role)).SetupWithManager(mgr); err != nil {
maocli, isLocalDevelopmentMode, role)).SetupWithManager(mgr); err != nil {
return fmt.Errorf("unable to create controller %s: %v", machine.ControllerName, err)
}
if err = (banner.NewReconciler(
@ -237,24 +232,23 @@ func operator(ctx context.Context, log *logrus.Entry) error {
}
if err = (serviceprincipalchecker.NewReconciler(
log.WithField("controller", serviceprincipalchecker.ControllerName),
arocli, kubernetescli, role)).SetupWithManager(mgr); err != nil {
kubernetescli, role)).SetupWithManager(mgr); err != nil {
return fmt.Errorf("unable to create controller %s: %v", serviceprincipalchecker.ControllerName, err)
}
if err = (clusterdnschecker.NewReconciler(
log.WithField("controller", clusterdnschecker.ControllerName),
arocli, operatorcli, role)).SetupWithManager(mgr); err != nil {
operatorcli, role)).SetupWithManager(mgr); err != nil {
return fmt.Errorf("unable to create controller %s: %v", clusterdnschecker.ControllerName, err)
}
if err = (ingresscertificatechecker.NewReconciler(
log.WithField("controller", ingresscertificatechecker.ControllerName),
arocli, operatorcli, configcli, role)).SetupWithManager(mgr); err != nil {
operatorcli, configcli, role)).SetupWithManager(mgr); err != nil {
return fmt.Errorf("unable to create controller %s: %v", ingresscertificatechecker.ControllerName, err)
}
}
if err = (internetchecker.NewReconciler(
log.WithField("controller", internetchecker.ControllerName),
arocli, role)).SetupWithManager(mgr); err != nil {
log.WithField("controller", internetchecker.ControllerName), role)).SetupWithManager(mgr); err != nil {
return fmt.Errorf("unable to create controller %s: %v", internetchecker.ControllerName, err)
}

Просмотреть файл

@ -20,7 +20,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/source"
arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1"
aroclient "github.com/Azure/ARO-RP/pkg/operator/clientset/versioned"
checkercommon "github.com/Azure/ARO-RP/pkg/operator/controllers/checkers/common"
"github.com/Azure/ARO-RP/pkg/util/conditions"
)
@ -40,18 +39,16 @@ type Reconciler struct {
log *logrus.Entry
role string
arocli aroclient.Interface
checker clusterDNSChecker
client client.Client
}
func NewReconciler(log *logrus.Entry, arocli aroclient.Interface, operatorcli operatorclient.Interface, role string) *Reconciler {
func NewReconciler(log *logrus.Entry, operatorcli operatorclient.Interface, role string) *Reconciler {
return &Reconciler{
log: log,
role: role,
arocli: arocli,
checker: newClusterDNSChecker(operatorcli),
}
}
@ -73,7 +70,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.
checkErr := r.checker.Check(ctx)
condition := r.condition(checkErr)
err = conditions.SetCondition(ctx, r.arocli, condition, r.role)
err = conditions.SetCondition(ctx, r.client, condition, r.role)
if err != nil {
return reconcile.Result{}, err
}
@ -91,7 +88,7 @@ func (r *Reconciler) reconcileDisabled(ctx context.Context) (ctrl.Result, error)
Status: operatorv1.ConditionUnknown,
}
return reconcile.Result{}, conditions.SetCondition(ctx, r.arocli, condition, r.role)
return reconcile.Result{}, conditions.SetCondition(ctx, r.client, condition, r.role)
}
func (r *Reconciler) condition(checkErr error) *operatorv1.OperatorCondition {

Просмотреть файл

@ -12,12 +12,12 @@ import (
operatorv1 "github.com/openshift/api/operator/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1"
arofake "github.com/Azure/ARO-RP/pkg/operator/clientset/versioned/fake"
checkercommon "github.com/Azure/ARO-RP/pkg/operator/controllers/checkers/common"
"github.com/Azure/ARO-RP/pkg/util/azureclient"
"github.com/Azure/ARO-RP/pkg/util/cmp"
@ -83,7 +83,6 @@ func TestReconcile(t *testing.T) {
}
clientFake := fake.NewClientBuilder().WithObjects(instance).Build()
arocliFake := arofake.NewSimpleClientset(instance)
r := &Reconciler{
log: utillog.GetLogger(),
@ -91,7 +90,6 @@ func TestReconcile(t *testing.T) {
checker: fakeChecker(func(ctx context.Context) error {
return tt.checkerReturnErr
}),
arocli: arocliFake,
client: clientFake,
}
@ -105,7 +103,7 @@ func TestReconcile(t *testing.T) {
t.Error(cmp.Diff(tt.wantResult, result))
}
instance, err = arocliFake.AroV1alpha1().Clusters().Get(ctx, arov1alpha1.SingletonClusterName, metav1.GetOptions{})
err = r.client.Get(ctx, types.NamespacedName{Name: arov1alpha1.SingletonClusterName}, instance)
if err != nil {
t.Fatal(err)
}

Просмотреть файл

@ -23,7 +23,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/source"
arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1"
aroclient "github.com/Azure/ARO-RP/pkg/operator/clientset/versioned"
checkercommon "github.com/Azure/ARO-RP/pkg/operator/controllers/checkers/common"
"github.com/Azure/ARO-RP/pkg/util/conditions"
)
@ -43,18 +42,16 @@ type Reconciler struct {
log *logrus.Entry
role string
arocli aroclient.Interface
checker ingressCertificateChecker
client client.Client
}
func NewReconciler(log *logrus.Entry, arocli aroclient.Interface, operatorcli operatorclient.Interface, configcli configclient.Interface, role string) *Reconciler {
func NewReconciler(log *logrus.Entry, operatorcli operatorclient.Interface, configcli configclient.Interface, role string) *Reconciler {
return &Reconciler{
log: log,
role: role,
arocli: arocli,
checker: newIngressCertificateChecker(operatorcli, configcli),
}
}
@ -76,7 +73,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.
checkErr := r.checker.Check(ctx)
condition := r.condition(checkErr)
err = conditions.SetCondition(ctx, r.arocli, condition, r.role)
err = conditions.SetCondition(ctx, r.client, condition, r.role)
if err != nil {
return reconcile.Result{}, err
}
@ -101,7 +98,7 @@ func (r *Reconciler) reconcileDisabled(ctx context.Context) (ctrl.Result, error)
Status: operatorv1.ConditionUnknown,
}
return reconcile.Result{}, conditions.SetCondition(ctx, r.arocli, condition, r.role)
return reconcile.Result{}, conditions.SetCondition(ctx, r.client, condition, r.role)
}
func (r *Reconciler) condition(checkErr error) *operatorv1.OperatorCondition {

Просмотреть файл

@ -12,12 +12,12 @@ import (
operatorv1 "github.com/openshift/api/operator/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1"
arofake "github.com/Azure/ARO-RP/pkg/operator/clientset/versioned/fake"
checkercommon "github.com/Azure/ARO-RP/pkg/operator/controllers/checkers/common"
"github.com/Azure/ARO-RP/pkg/util/azureclient"
"github.com/Azure/ARO-RP/pkg/util/cmp"
@ -90,7 +90,6 @@ func TestReconcile(t *testing.T) {
}
clientFake := fake.NewClientBuilder().WithObjects(instance).Build()
arocliFake := arofake.NewSimpleClientset(instance)
r := &Reconciler{
log: utillog.GetLogger(),
@ -99,7 +98,6 @@ func TestReconcile(t *testing.T) {
return tt.checkerReturnErr
}),
client: clientFake,
arocli: arocliFake,
}
result, err := r.Reconcile(ctx, ctrl.Request{})
@ -112,7 +110,7 @@ func TestReconcile(t *testing.T) {
t.Error(cmp.Diff(tt.wantResult, result))
}
instance, err = arocliFake.AroV1alpha1().Clusters().Get(ctx, arov1alpha1.SingletonClusterName, metav1.GetOptions{})
err = r.client.Get(ctx, types.NamespacedName{Name: arov1alpha1.SingletonClusterName}, instance)
if err != nil {
t.Fatal(err)
}

Просмотреть файл

@ -17,7 +17,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1"
aroclient "github.com/Azure/ARO-RP/pkg/operator/clientset/versioned"
checkercommon "github.com/Azure/ARO-RP/pkg/operator/controllers/checkers/common"
"github.com/Azure/ARO-RP/pkg/util/conditions"
)
@ -37,18 +36,16 @@ type Reconciler struct {
log *logrus.Entry
role string
arocli aroclient.Interface
checker internetChecker
client client.Client
}
func NewReconciler(log *logrus.Entry, arocli aroclient.Interface, role string) *Reconciler {
func NewReconciler(log *logrus.Entry, role string) *Reconciler {
return &Reconciler{
log: log,
role: role,
arocli: arocli,
checker: newInternetChecker(),
}
}
@ -70,7 +67,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.
checkErr := r.checker.Check(instance.Spec.InternetChecker.URLs)
condition := r.condition(checkErr)
err = conditions.SetCondition(ctx, r.arocli, condition, r.role)
err = conditions.SetCondition(ctx, r.client, condition, r.role)
if err != nil {
return reconcile.Result{}, err
}
@ -88,7 +85,7 @@ func (r *Reconciler) reconcileDisabled(ctx context.Context) (ctrl.Result, error)
Status: operatorv1.ConditionUnknown,
}
return reconcile.Result{}, conditions.SetCondition(ctx, r.arocli, condition, r.role)
return reconcile.Result{}, conditions.SetCondition(ctx, r.client, condition, r.role)
}
func (r *Reconciler) condition(checkErr error) *operatorv1.OperatorCondition {

Просмотреть файл

@ -12,13 +12,13 @@ import (
operatorv1 "github.com/openshift/api/operator/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"github.com/Azure/ARO-RP/pkg/operator"
arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1"
arofake "github.com/Azure/ARO-RP/pkg/operator/clientset/versioned/fake"
checkercommon "github.com/Azure/ARO-RP/pkg/operator/controllers/checkers/common"
"github.com/Azure/ARO-RP/pkg/util/cmp"
utillog "github.com/Azure/ARO-RP/pkg/util/log"
@ -90,7 +90,6 @@ func TestReconcile(t *testing.T) {
}
clientFake := fake.NewClientBuilder().WithObjects(instance).Build()
arocliFake := arofake.NewSimpleClientset(instance)
r := &Reconciler{
log: utillog.GetLogger(),
@ -102,7 +101,6 @@ func TestReconcile(t *testing.T) {
return tt.checkerReturnErr
}),
arocli: arocliFake,
client: clientFake,
}
@ -116,7 +114,7 @@ func TestReconcile(t *testing.T) {
t.Error(cmp.Diff(tt.wantResult, result))
}
instance, err = arocliFake.AroV1alpha1().Clusters().Get(ctx, arov1alpha1.SingletonClusterName, metav1.GetOptions{})
err = r.client.Get(ctx, types.NamespacedName{Name: arov1alpha1.SingletonClusterName}, instance)
if err != nil {
t.Fatal(err)
}

Просмотреть файл

@ -21,7 +21,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/source"
arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1"
aroclient "github.com/Azure/ARO-RP/pkg/operator/clientset/versioned"
checkercommon "github.com/Azure/ARO-RP/pkg/operator/controllers/checkers/common"
"github.com/Azure/ARO-RP/pkg/util/clusterauthorizer"
"github.com/Azure/ARO-RP/pkg/util/conditions"
@ -42,18 +41,16 @@ type Reconciler struct {
log *logrus.Entry
role string
arocli aroclient.Interface
checker servicePrincipalChecker
client client.Client
}
func NewReconciler(log *logrus.Entry, arocli aroclient.Interface, kubernetescli kubernetes.Interface, role string) *Reconciler {
func NewReconciler(log *logrus.Entry, kubernetescli kubernetes.Interface, role string) *Reconciler {
return &Reconciler{
log: log,
role: role,
arocli: arocli,
checker: newServicePrincipalChecker(log, kubernetescli),
}
}
@ -75,7 +72,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.
checkErr := r.checker.Check(ctx, instance.Spec.AZEnvironment)
condition := r.condition(checkErr)
err = conditions.SetCondition(ctx, r.arocli, condition, r.role)
err = conditions.SetCondition(ctx, r.client, condition, r.role)
if err != nil {
return reconcile.Result{}, err
}
@ -93,7 +90,7 @@ func (r *Reconciler) reconcileDisabled(ctx context.Context) (ctrl.Result, error)
Status: operatorv1.ConditionUnknown,
}
return reconcile.Result{}, conditions.SetCondition(ctx, r.arocli, condition, r.role)
return reconcile.Result{}, conditions.SetCondition(ctx, r.client, condition, r.role)
}
func (r *Reconciler) condition(checkErr error) *operatorv1.OperatorCondition {

Просмотреть файл

@ -12,12 +12,12 @@ import (
operatorv1 "github.com/openshift/api/operator/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1"
arofake "github.com/Azure/ARO-RP/pkg/operator/clientset/versioned/fake"
checkercommon "github.com/Azure/ARO-RP/pkg/operator/controllers/checkers/common"
"github.com/Azure/ARO-RP/pkg/util/azureclient"
"github.com/Azure/ARO-RP/pkg/util/cmp"
@ -83,7 +83,6 @@ func TestReconcile(t *testing.T) {
}
clientFake := fake.NewClientBuilder().WithObjects(instance).Build()
arocliFake := arofake.NewSimpleClientset(instance)
r := &Reconciler{
log: utillog.GetLogger(),
@ -95,7 +94,6 @@ func TestReconcile(t *testing.T) {
return tt.checkerReturnErr
}),
arocli: arocliFake,
client: clientFake,
}
@ -109,7 +107,7 @@ func TestReconcile(t *testing.T) {
t.Error(cmp.Diff(tt.wantResult, result))
}
instance, err = arocliFake.AroV1alpha1().Clusters().Get(ctx, arov1alpha1.SingletonClusterName, metav1.GetOptions{})
err = r.client.Get(ctx, types.NamespacedName{Name: arov1alpha1.SingletonClusterName}, instance)
if err != nil {
t.Fatal(err)
}

Просмотреть файл

@ -18,7 +18,6 @@ import (
ctrlfake "sigs.k8s.io/controller-runtime/pkg/client/fake"
arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1"
arofake "github.com/Azure/ARO-RP/pkg/operator/clientset/versioned/fake"
_ "github.com/Azure/ARO-RP/pkg/util/scheme"
"github.com/Azure/ARO-RP/pkg/util/version"
testdatabase "github.com/Azure/ARO-RP/test/database"
@ -37,7 +36,6 @@ func TestGenevaLoggingDaemonset(t *testing.T) {
tests := []struct {
name string
request ctrl.Request
arocli *arofake.Clientset
operatorFlags arov1alpha1.OperatorFlags
validateDaemonset func(*appsv1.DaemonSet) []error
}{

Просмотреть файл

@ -17,7 +17,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1"
aroclient "github.com/Azure/ARO-RP/pkg/operator/clientset/versioned"
"github.com/Azure/ARO-RP/pkg/util/conditions"
)
@ -30,7 +29,6 @@ const (
type Reconciler struct {
log *logrus.Entry
arocli aroclient.Interface
maocli machineclient.Interface
isLocalDevelopmentMode bool
@ -39,10 +37,9 @@ type Reconciler struct {
client client.Client
}
func NewReconciler(log *logrus.Entry, arocli aroclient.Interface, maocli machineclient.Interface, isLocalDevelopmentMode bool, role string) *Reconciler {
func NewReconciler(log *logrus.Entry, maocli machineclient.Interface, isLocalDevelopmentMode bool, role string) *Reconciler {
return &Reconciler{
log: log,
arocli: arocli,
maocli: maocli,
isLocalDevelopmentMode: isLocalDevelopmentMode,
role: role,
@ -83,7 +80,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.
cond.Message = sb.String()
}
return reconcile.Result{}, conditions.SetCondition(ctx, r.arocli, cond, r.role)
return reconcile.Result{}, conditions.SetCondition(ctx, r.client, cond, r.role)
}
func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {

Просмотреть файл

@ -16,11 +16,11 @@ import (
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1"
arofake "github.com/Azure/ARO-RP/pkg/operator/clientset/versioned/fake"
_ "github.com/Azure/ARO-RP/pkg/util/scheme"
)
@ -72,7 +72,6 @@ func TestMachineReconciler(t *testing.T) {
name string
request ctrl.Request
maocli *machinefake.Clientset
arocli *arofake.Clientset
wantConditions []operatorv1.OperatorCondition
}{
{
@ -148,6 +147,8 @@ func TestMachineReconciler(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
baseCluster := arov1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{Name: "cluster"},
Status: arov1alpha1.ClusterStatus{Conditions: []operatorv1.OperatorCondition{}},
@ -159,23 +160,22 @@ func TestMachineReconciler(t *testing.T) {
}
clientFake := fake.NewClientBuilder().WithObjects(&baseCluster).Build()
arocliFake := arofake.NewSimpleClientset(&baseCluster)
r := &Reconciler{
maocli: tt.maocli,
log: logrus.NewEntry(logrus.StandardLogger()),
arocli: arocliFake,
isLocalDevelopmentMode: false,
role: "master",
client: clientFake,
}
_, err := r.Reconcile(context.Background(), tt.request)
_, err := r.Reconcile(ctx, tt.request)
if err != nil {
t.Fatal(err)
}
cluster, err := r.arocli.AroV1alpha1().Clusters().Get(context.Background(), arov1alpha1.SingletonClusterName, metav1.GetOptions{})
cluster := &arov1alpha1.Cluster{}
err = r.client.Get(ctx, types.NamespacedName{Name: arov1alpha1.SingletonClusterName}, cluster)
if err != nil {
t.Fatal(err)
}

Просмотреть файл

@ -8,12 +8,13 @@ import (
operatorv1 "github.com/openshift/api/operator/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/clock"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/Azure/ARO-RP/pkg/operator"
arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1"
aroclient "github.com/Azure/ARO-RP/pkg/operator/clientset/versioned"
"github.com/Azure/ARO-RP/pkg/util/version"
)
@ -21,13 +22,14 @@ import (
// This variable makes it easier to test conditions.
var kubeclock clock.Clock = &clock.RealClock{}
// TODO: Need to get rid of dependency on aroclient.Interface and replace it with a client from controller-runtime.
func SetCondition(ctx context.Context, arocli aroclient.Interface, cond *operatorv1.OperatorCondition, role string) error {
func SetCondition(ctx context.Context, c client.Client, cond *operatorv1.OperatorCondition, role string) error {
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
if cond == nil {
return nil
}
cluster, err := arocli.AroV1alpha1().Clusters().Get(ctx, arov1alpha1.SingletonClusterName, metav1.GetOptions{})
cluster := &arov1alpha1.Cluster{}
err := c.Get(ctx, types.NamespacedName{Name: arov1alpha1.SingletonClusterName}, cluster)
if err != nil {
return err
}
@ -43,8 +45,7 @@ func SetCondition(ctx context.Context, arocli aroclient.Interface, cond *operato
return nil
}
_, err = arocli.AroV1alpha1().Clusters().UpdateStatus(ctx, cluster, metav1.UpdateOptions{})
return err
return c.Status().Update(ctx, cluster)
})
}

Просмотреть файл

@ -5,19 +5,19 @@ package conditions
import (
"context"
"reflect"
"testing"
"time"
operatorv1 "github.com/openshift/api/operator/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/clock"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1"
aroclient "github.com/Azure/ARO-RP/pkg/operator/clientset/versioned"
arofake "github.com/Azure/ARO-RP/pkg/operator/clientset/versioned/fake"
"github.com/Azure/ARO-RP/pkg/util/cmp"
_ "github.com/Azure/ARO-RP/pkg/util/scheme"
)
func TestSetCondition(t *testing.T) {
@ -26,13 +26,12 @@ func TestSetCondition(t *testing.T) {
objectName := "cluster"
version := "unknown"
kubeclock = &clock.FakeClock{}
kubeclock = clock.NewFakeClock(time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC))
var transitionTime = metav1.Time{Time: kubeclock.Now()}
for _, tt := range []struct {
name string
aroclient aroclient.Interface
objects []kruntime.Object
objects []client.Object
input *operatorv1.OperatorCondition
expected arov1alpha1.ClusterStatus
@ -40,7 +39,7 @@ func TestSetCondition(t *testing.T) {
}{
{
name: "no condition provided",
objects: []kruntime.Object{&arov1alpha1.Cluster{
objects: []client.Object{&arov1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: objectName,
},
@ -49,7 +48,7 @@ func TestSetCondition(t *testing.T) {
},
{
name: "new condition provided",
objects: []kruntime.Object{&arov1alpha1.Cluster{
objects: []client.Object{&arov1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: objectName,
},
@ -67,6 +66,7 @@ func TestSetCondition(t *testing.T) {
{
Type: arov1alpha1.InternetReachableFromMaster,
Status: operatorv1.ConditionFalse,
LastTransitionTime: transitionTime,
},
},
OperatorVersion: version,
@ -74,7 +74,7 @@ func TestSetCondition(t *testing.T) {
},
{
name: "condition provided without status change - only update operator version",
objects: []kruntime.Object{&arov1alpha1.Cluster{
objects: []client.Object{&arov1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: objectName,
},
@ -104,7 +104,7 @@ func TestSetCondition(t *testing.T) {
},
{
name: "condition provided without status change - no update",
objects: []kruntime.Object{&arov1alpha1.Cluster{
objects: []client.Object{&arov1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: objectName,
},
@ -113,7 +113,7 @@ func TestSetCondition(t *testing.T) {
{
Type: arov1alpha1.InternetReachableFromMaster,
Status: operatorv1.ConditionFalse,
LastTransitionTime: metav1.Time{Time: time.Date(1970, 0, 0, 0, 0, 0, 0, time.UTC)},
LastTransitionTime: metav1.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
},
},
OperatorVersion: version,
@ -122,14 +122,14 @@ func TestSetCondition(t *testing.T) {
input: &operatorv1.OperatorCondition{
Type: arov1alpha1.InternetReachableFromMaster,
Status: operatorv1.ConditionFalse,
LastTransitionTime: metav1.Time{Time: time.Date(2021, 0, 0, 0, 0, 0, 0, time.UTC)},
LastTransitionTime: metav1.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
},
expected: arov1alpha1.ClusterStatus{
Conditions: []operatorv1.OperatorCondition{
{
Type: arov1alpha1.InternetReachableFromMaster,
Status: operatorv1.ConditionFalse,
LastTransitionTime: metav1.Time{Time: time.Date(1970, 0, 0, 0, 0, 0, 0, time.UTC)},
LastTransitionTime: metav1.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
},
},
OperatorVersion: version,
@ -137,7 +137,7 @@ func TestSetCondition(t *testing.T) {
},
{
name: "update one of the existing conditions",
objects: []kruntime.Object{&arov1alpha1.Cluster{
objects: []client.Object{&arov1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: objectName,
},
@ -176,7 +176,7 @@ func TestSetCondition(t *testing.T) {
},
{
name: "cleanup stale conditions",
objects: []kruntime.Object{&arov1alpha1.Cluster{
objects: []client.Object{&arov1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: objectName,
},
@ -207,20 +207,28 @@ func TestSetCondition(t *testing.T) {
},
} {
t.Run(tt.name, func(t *testing.T) {
client := arofake.NewSimpleClientset(tt.objects...)
clientFake := fake.NewClientBuilder().WithObjects(tt.objects...).Build()
err := SetCondition(ctx, client, tt.input, role)
err := SetCondition(ctx, clientFake, tt.input, role)
if err != nil && tt.wantErr != nil {
t.Fatal(err.Error())
}
result, err := client.AroV1alpha1().Clusters().Get(ctx, objectName, metav1.GetOptions{})
result := &arov1alpha1.Cluster{}
err = clientFake.Get(ctx, types.NamespacedName{Name: arov1alpha1.SingletonClusterName}, result)
if err != nil {
t.Fatal(err.Error())
}
if !reflect.DeepEqual(result.Status, tt.expected) {
t.Fatal(cmp.Diff(result.Status, tt.expected))
// cmp.Diff correctly compares times the same time, but in different timezones
// unlike reflect.DeepEqual which compares field by field.
// We need this because fake client marshals and unmarshals objects
// due to this line[1] in apimachiner time than gets converted to a local time.
//
// [1] https://github.com/kubernetes/apimachinery/blob/24bec8a7ae9ed9efe31aa9239cc616d751c2bc69/pkg/apis/meta/v1/time.go#L115
diff := cmp.Diff(result.Status, tt.expected)
if diff != "" {
t.Fatal(diff)
}
})
}