Removes dependency on versioned config client

This commit is contained in:
Mikalai Radchuk 2023-01-13 16:35:23 +00:00 коммит произвёл Mikalai Radchuk
Родитель c19247339b
Коммит e3cdc1342d
13 изменённых файлов: 95 добавлений и 104 удалений

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

@ -8,7 +8,6 @@ import (
"flag"
"fmt"
configclient "github.com/openshift/client-go/config/clientset/versioned"
imageregistryclient "github.com/openshift/client-go/imageregistry/clientset/versioned"
machineclient "github.com/openshift/client-go/machine/clientset/versioned"
operatorclient "github.com/openshift/client-go/operator/clientset/versioned"
@ -81,10 +80,6 @@ func operator(ctx context.Context, log *logrus.Entry) error {
client := mgr.GetClient()
configcli, err := configclient.NewForConfig(restConfig)
if err != nil {
return err
}
kubernetescli, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return err
@ -123,7 +118,7 @@ func operator(ctx context.Context, log *logrus.Entry) error {
}
if err = (clusteroperatoraro.NewReconciler(
log.WithField("controller", clusteroperatoraro.ControllerName),
client, configcli)).SetupWithManager(mgr); err != nil {
client)).SetupWithManager(mgr); err != nil {
return fmt.Errorf("unable to create controller %s: %v", clusteroperatoraro.ControllerName, err)
}
if err = (pullsecret.NewReconciler(
@ -138,12 +133,12 @@ func operator(ctx context.Context, log *logrus.Entry) error {
}
if err = (workaround.NewReconciler(
log.WithField("controller", workaround.ControllerName),
client, configcli, kubernetescli, mcocli, restConfig)).SetupWithManager(mgr); err != nil {
client, kubernetescli, mcocli, restConfig)).SetupWithManager(mgr); err != nil {
return fmt.Errorf("unable to create controller %s: %v", workaround.ControllerName, err)
}
if err = (routefix.NewReconciler(
log.WithField("controller", routefix.ControllerName),
client, configcli, kubernetescli, securitycli, restConfig)).SetupWithManager(mgr); err != nil {
client, kubernetescli, securitycli, restConfig)).SetupWithManager(mgr); err != nil {
return fmt.Errorf("unable to create controller %s: %v", routefix.ControllerName, err)
}
if err = (monitoring.NewReconciler(
@ -198,7 +193,7 @@ func operator(ctx context.Context, log *logrus.Entry) error {
}
if err = (imageconfig.NewReconciler(
log.WithField("controller", imageconfig.ControllerName),
client, configcli)).SetupWithManager(mgr); err != nil {
client)).SetupWithManager(mgr); err != nil {
return fmt.Errorf("unable to create controller %s: %v", imageconfig.ControllerName, err)
}
if err = (previewfeature.NewReconciler(
@ -243,7 +238,7 @@ func operator(ctx context.Context, log *logrus.Entry) error {
}
if err = (ingresscertificatechecker.NewReconciler(
log.WithField("controller", ingresscertificatechecker.ControllerName),
client, operatorcli, configcli, role)).SetupWithManager(mgr); err != nil {
client, operatorcli, role)).SetupWithManager(mgr); err != nil {
return fmt.Errorf("unable to create controller %s: %v", ingresscertificatechecker.ControllerName, err)
}
}

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

@ -10,6 +10,7 @@ import (
configclient "github.com/openshift/client-go/config/clientset/versioned"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
@ -54,7 +55,11 @@ func writeVersion(ctx context.Context, restconfig *rest.Config) error {
return err
}
clusterVersion, err := version.GetClusterVersion(ctx, configcli)
cv, err := configcli.ConfigV1().ClusterVersions().Get(ctx, "version", metav1.GetOptions{})
if err != nil {
return err
}
clusterVersion, err := version.GetClusterVersion(cv)
if err != nil {
return err
}

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

@ -90,7 +90,11 @@ func (m *manager) removePrivateDNSZone(ctx context.Context) error {
return nil
}
v, err := version.GetClusterVersion(ctx, m.configcli)
cv, err := m.configcli.ConfigV1().ClusterVersions().Get(ctx, "version", metav1.GetOptions{})
if err != nil {
return err
}
v, err := version.GetClusterVersion(cv)
if err != nil {
m.log.Print(err)
return nil

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

@ -15,9 +15,11 @@ import (
"errors"
"fmt"
configclient "github.com/openshift/client-go/config/clientset/versioned"
configv1 "github.com/openshift/api/config/v1"
operatorclient "github.com/openshift/client-go/operator/clientset/versioned"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
var (
@ -37,19 +39,20 @@ type ingressCertificateChecker interface {
}
type checker struct {
client client.Client
operatorcli operatorclient.Interface
configcli configclient.Interface
}
func newIngressCertificateChecker(operatorcli operatorclient.Interface, configcli configclient.Interface) *checker {
func newIngressCertificateChecker(client client.Client, operatorcli operatorclient.Interface) *checker {
return &checker{
client: client,
operatorcli: operatorcli,
configcli: configcli,
}
}
func (r *checker) Check(ctx context.Context) error {
cv, err := r.configcli.ConfigV1().ClusterVersions().Get(ctx, "version", metav1.GetOptions{})
cv := &configv1.ClusterVersion{}
err := r.client.Get(ctx, types.NamespacedName{Name: "version"}, cv)
if err != nil {
return err
}

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

@ -13,6 +13,7 @@ import (
operatorfake "github.com/openshift/client-go/operator/clientset/versioned/fake"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrlfake "sigs.k8s.io/controller-runtime/pkg/client/fake"
)
func TestCheck(t *testing.T) {
@ -74,9 +75,14 @@ func TestCheck(t *testing.T) {
configcliFake.Tracker().Add(tt.clusterVersion)
}
clientBuilder := ctrlfake.NewClientBuilder()
if tt.clusterVersion != nil {
clientBuilder = clientBuilder.WithObjects(tt.clusterVersion)
}
sp := &checker{
client: clientBuilder.Build(),
operatorcli: operatorcliFake,
configcli: configcliFake,
}
err := sp.Check(ctx)

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

@ -10,7 +10,6 @@ import (
configv1 "github.com/openshift/api/config/v1"
operatorv1 "github.com/openshift/api/operator/v1"
configclient "github.com/openshift/client-go/config/clientset/versioned"
operatorclient "github.com/openshift/client-go/operator/clientset/versioned"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/types"
@ -47,12 +46,12 @@ type Reconciler struct {
client client.Client
}
func NewReconciler(log *logrus.Entry, client client.Client, operatorcli operatorclient.Interface, configcli configclient.Interface, role string) *Reconciler {
func NewReconciler(log *logrus.Entry, client client.Client, operatorcli operatorclient.Interface, role string) *Reconciler {
return &Reconciler{
log: log,
role: role,
checker: newIngressCertificateChecker(operatorcli, configcli),
checker: newIngressCertificateChecker(client, operatorcli),
client: client,
}

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

@ -7,7 +7,6 @@ import (
"context"
configv1 "github.com/openshift/api/config/v1"
configclient "github.com/openshift/client-go/config/clientset/versioned"
"github.com/openshift/library-go/pkg/config/clusteroperator/v1helpers"
"github.com/sirupsen/logrus"
kerrors "k8s.io/apimachinery/pkg/api/errors"
@ -40,20 +39,16 @@ const (
)
type Reconciler struct {
// TODO: Replace configcli with CO client
log *logrus.Entry
configcli configclient.Interface
client client.Client
}
// TODO: Decide whether we actually going to make any progress on this. If not - clean up.
func NewReconciler(log *logrus.Entry, client client.Client, configcli configclient.Interface) *Reconciler {
func NewReconciler(log *logrus.Entry, client client.Client) *Reconciler {
return &Reconciler{
log: log,
configcli: configcli,
client: client,
log: log,
client: client,
}
}
@ -76,7 +71,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.
return err
}
co, err = r.configcli.ConfigV1().ClusterOperators().Update(ctx, co, metav1.UpdateOptions{})
err = r.client.Update(ctx, co)
if err != nil {
return err
}
@ -119,26 +114,25 @@ func (r *Reconciler) setClusterOperatorStatus(ctx context.Context, co *configv1.
v1helpers.SetStatusCondition(&co.Status.Conditions, c)
}
_, err := r.configcli.ConfigV1().ClusterOperators().UpdateStatus(ctx, co, metav1.UpdateOptions{})
return err
return r.client.Status().Update(ctx, co)
}
func (r *Reconciler) getOrCreateClusterOperator(ctx context.Context) (*configv1.ClusterOperator, error) {
co, err := r.configcli.ConfigV1().ClusterOperators().Get(ctx, clusterOperatorName, metav1.GetOptions{})
co := &configv1.ClusterOperator{}
err := r.client.Get(ctx, types.NamespacedName{Name: clusterOperatorName}, co)
if !kerrors.IsNotFound(err) {
return co, err
}
r.log.Infof("ClusterOperator does not exist, creating a new one.")
defaultCo := r.defaultOperator()
co, err = r.configcli.ConfigV1().ClusterOperators().Create(ctx, defaultCo, metav1.CreateOptions{})
co = r.defaultOperator()
err = r.client.Create(ctx, co)
if err != nil {
return nil, err
}
co.Status = defaultCo.Status
return r.configcli.ConfigV1().ClusterOperators().UpdateStatus(ctx, co, metav1.UpdateOptions{})
err = r.client.Status().Update(ctx, co)
return co, err
}
func (r *Reconciler) defaultOperator() *configv1.ClusterOperator {

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

@ -11,9 +11,7 @@ import (
"github.com/Azure/go-autorest/autorest/azure"
configv1 "github.com/openshift/api/config/v1"
configclient "github.com/openshift/client-go/config/clientset/versioned"
"github.com/sirupsen/logrus"
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/builder"
@ -37,16 +35,13 @@ const (
type Reconciler struct {
log *logrus.Entry
configcli configclient.Interface
client client.Client
}
func NewReconciler(log *logrus.Entry, client client.Client, configcli configclient.Interface) *Reconciler {
func NewReconciler(log *logrus.Entry, client client.Client) *Reconciler {
return &Reconciler{
log: log,
configcli: configcli,
client: client,
log: log,
client: client,
}
}
@ -74,7 +69,8 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.
}
// Get image.config yaml
imageconfig, err := r.configcli.ConfigV1().Images().Get(ctx, request.Name, metav1.GetOptions{})
imageconfig := &configv1.Image{}
err = r.client.Get(ctx, types.NamespacedName{Name: request.Name}, imageconfig)
if err != nil {
return reconcile.Result{}, err
}
@ -106,8 +102,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.
}
// Update image config registry
_, err = r.configcli.ConfigV1().Images().Update(ctx, imageconfig, metav1.UpdateOptions{})
return reconcile.Result{}, err
return reconcile.Result{}, r.client.Update(ctx, imageconfig)
}
// SetupWithManager setup the manager

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

@ -10,10 +10,9 @@ import (
"testing"
configv1 "github.com/openshift/api/config/v1"
configclient "github.com/openshift/client-go/config/clientset/versioned"
configfake "github.com/openshift/client-go/config/clientset/versioned/fake"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
ctrlfake "sigs.k8s.io/controller-runtime/pkg/client/fake"
@ -28,7 +27,7 @@ func TestImageConfigReconciler(t *testing.T) {
type test struct {
name string
instance *arov1alpha1.Cluster
configcli configclient.Interface
image *configv1.Image
wantRegistrySources configv1.RegistrySources
wantErr string
}
@ -47,7 +46,7 @@ func TestImageConfigReconciler(t *testing.T) {
Location: "eastus",
},
},
configcli: configfake.NewSimpleClientset(&configv1.Image{
image: &configv1.Image{
ObjectMeta: metav1.ObjectMeta{Name: arov1alpha1.SingletonClusterName},
Spec: configv1.ImageSpec{
RegistrySources: configv1.RegistrySources{
@ -56,7 +55,7 @@ func TestImageConfigReconciler(t *testing.T) {
},
},
},
}),
},
wantRegistrySources: configv1.RegistrySources{
AllowedRegistries: []string{
"quay.io",
@ -65,14 +64,14 @@ func TestImageConfigReconciler(t *testing.T) {
},
{
name: "Image config registry source is empty, no action",
configcli: configfake.NewSimpleClientset(&configv1.Image{
image: &configv1.Image{
ObjectMeta: metav1.ObjectMeta{Name: arov1alpha1.SingletonClusterName},
}),
},
wantRegistrySources: configv1.RegistrySources{},
},
{
name: "allowedRegistries exists with duplicates, function should appropriately add registries",
configcli: configfake.NewSimpleClientset(&configv1.Image{
image: &configv1.Image{
ObjectMeta: metav1.ObjectMeta{Name: arov1alpha1.SingletonClusterName},
Spec: configv1.ImageSpec{
RegistrySources: configv1.RegistrySources{
@ -83,7 +82,7 @@ func TestImageConfigReconciler(t *testing.T) {
},
},
},
}),
},
wantRegistrySources: configv1.RegistrySources{
AllowedRegistries: []string{
"quay.io",
@ -94,7 +93,7 @@ func TestImageConfigReconciler(t *testing.T) {
},
{
name: "blockedRegistries exists, function should delete registries",
configcli: configfake.NewSimpleClientset(&configv1.Image{
image: &configv1.Image{
ObjectMeta: metav1.ObjectMeta{Name: arov1alpha1.SingletonClusterName},
Spec: configv1.ImageSpec{
RegistrySources: configv1.RegistrySources{
@ -105,7 +104,7 @@ func TestImageConfigReconciler(t *testing.T) {
},
},
},
}),
},
wantRegistrySources: configv1.RegistrySources{
BlockedRegistries: []string{
"quay.io",
@ -122,7 +121,7 @@ func TestImageConfigReconciler(t *testing.T) {
},
},
},
configcli: configfake.NewSimpleClientset(&configv1.Image{
image: &configv1.Image{
ObjectMeta: metav1.ObjectMeta{Name: arov1alpha1.SingletonClusterName},
Spec: configv1.ImageSpec{
RegistrySources: configv1.RegistrySources{
@ -131,7 +130,7 @@ func TestImageConfigReconciler(t *testing.T) {
},
},
},
}),
},
wantRegistrySources: configv1.RegistrySources{
AllowedRegistries: []string{
"quay.io",
@ -140,7 +139,7 @@ func TestImageConfigReconciler(t *testing.T) {
},
{
name: "Both AllowedRegistries and BlockedRegistries are present, function should fail silently and not requeue",
configcli: configfake.NewSimpleClientset(&configv1.Image{
image: &configv1.Image{
ObjectMeta: metav1.ObjectMeta{Name: arov1alpha1.SingletonClusterName},
Spec: configv1.ImageSpec{
RegistrySources: configv1.RegistrySources{
@ -153,7 +152,7 @@ func TestImageConfigReconciler(t *testing.T) {
},
},
},
}),
},
wantRegistrySources: configv1.RegistrySources{
BlockedRegistries: []string{
"arointsvc.azurecr.io",
@ -178,14 +177,14 @@ func TestImageConfigReconciler(t *testing.T) {
Location: "anyplace",
},
},
configcli: configfake.NewSimpleClientset(&configv1.Image{
image: &configv1.Image{
ObjectMeta: metav1.ObjectMeta{Name: arov1alpha1.SingletonClusterName},
Spec: configv1.ImageSpec{
RegistrySources: configv1.RegistrySources{
AllowedRegistries: []string{"quay.io"},
},
},
}),
},
wantRegistrySources: configv1.RegistrySources{
AllowedRegistries: []string{
"quay.io",
@ -207,14 +206,14 @@ func TestImageConfigReconciler(t *testing.T) {
Location: "anyplace",
},
},
configcli: configfake.NewSimpleClientset(&configv1.Image{
image: &configv1.Image{
ObjectMeta: metav1.ObjectMeta{Name: arov1alpha1.SingletonClusterName},
Spec: configv1.ImageSpec{
RegistrySources: configv1.RegistrySources{
AllowedRegistries: []string{"quay.io"},
},
},
}),
},
wantRegistrySources: configv1.RegistrySources{
AllowedRegistries: []string{
"quay.io",
@ -242,10 +241,11 @@ func TestImageConfigReconciler(t *testing.T) {
instance = tt.instance
}
clientFake := ctrlfake.NewClientBuilder().WithObjects(instance, tt.image).Build()
r := &Reconciler{
log: logrus.NewEntry(logrus.StandardLogger()),
configcli: tt.configcli,
client: ctrlfake.NewClientBuilder().WithObjects(instance).Build(),
log: logrus.NewEntry(logrus.StandardLogger()),
client: clientFake,
}
request := ctrl.Request{}
request.Name = "cluster"
@ -255,7 +255,9 @@ func TestImageConfigReconciler(t *testing.T) {
err == nil && tt.wantErr != "" {
t.Error(err)
}
imgcfg, err := r.configcli.ConfigV1().Images().Get(ctx, request.Name, metav1.GetOptions{})
imgcfg := &configv1.Image{}
err = r.client.Get(ctx, types.NamespacedName{Name: request.Name}, imgcfg)
if err != nil {
t.Fatal(err)
}

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

@ -6,8 +6,8 @@ package routefix
import (
"context"
configv1 "github.com/openshift/api/config/v1"
securityv1 "github.com/openshift/api/security/v1"
configclient "github.com/openshift/client-go/config/clientset/versioned"
securityclient "github.com/openshift/client-go/security/clientset/versioned"
"github.com/sirupsen/logrus"
appsv1 "k8s.io/api/apps/v1"
@ -38,7 +38,6 @@ const (
type Reconciler struct {
log *logrus.Entry
configcli configclient.Interface
kubernetescli kubernetes.Interface
securitycli securityclient.Interface
@ -55,10 +54,9 @@ var (
)
// NewReconciler creates a new Reconciler
func NewReconciler(log *logrus.Entry, client client.Client, configcli configclient.Interface, kubernetescli kubernetes.Interface, securitycli securityclient.Interface, restConfig *rest.Config) *Reconciler {
func NewReconciler(log *logrus.Entry, client client.Client, kubernetescli kubernetes.Interface, securitycli securityclient.Interface, restConfig *rest.Config) *Reconciler {
return &Reconciler{
log: log,
configcli: configcli,
kubernetescli: kubernetescli,
securitycli: securitycli,
client: client,
@ -86,7 +84,12 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.
// cluster version is not set to final until upgrade is completed. We need to
// detect if desired version is with the fix, so we can prevent stuck upgrade
// by deleting fix resources
clusterVersion, err := version.GetClusterDesiredVersion(ctx, r.configcli)
cv := &configv1.ClusterVersion{}
err = r.client.Get(ctx, types.NamespacedName{Name: "version"}, cv)
if err != nil {
return reconcile.Result{}, err
}
clusterVersion, err := version.ParseVersion(cv.Status.Desired.Version)
if err != nil {
r.log.Errorf("error getting the OpenShift desired version: %v", err)
return reconcile.Result{}, err

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

@ -7,7 +7,7 @@ import (
"context"
"time"
configclient "github.com/openshift/client-go/config/clientset/versioned"
configv1 "github.com/openshift/api/config/v1"
mcoclient "github.com/openshift/machine-config-operator/pkg/generated/clientset/versioned"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/types"
@ -33,14 +33,12 @@ const (
type Reconciler struct {
log *logrus.Entry
configcli configclient.Interface
workarounds []Workaround
client client.Client
}
func NewReconciler(log *logrus.Entry, client client.Client, configcli configclient.Interface, kubernetescli kubernetes.Interface, mcocli mcoclient.Interface, restConfig *rest.Config) *Reconciler {
func NewReconciler(log *logrus.Entry, client client.Client, kubernetescli kubernetes.Interface, mcocli mcoclient.Interface, restConfig *rest.Config) *Reconciler {
dh, err := dynamichelper.New(log, restConfig)
if err != nil {
panic(err)
@ -48,7 +46,6 @@ func NewReconciler(log *logrus.Entry, client client.Client, configcli configclie
return &Reconciler{
log: log,
configcli: configcli,
workarounds: []Workaround{NewSystemReserved(log, mcocli, dh), NewIfReload(log, kubernetescli)},
client: client,
}
@ -68,7 +65,14 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.
}
r.log.Debug("running")
clusterVersion, err := version.GetClusterVersion(ctx, r.configcli)
cv := &configv1.ClusterVersion{}
err = r.client.Get(ctx, types.NamespacedName{Name: "version"}, cv)
if err != nil {
return reconcile.Result{}, err
}
clusterVersion, err := version.GetClusterVersion(cv)
if err != nil {
r.log.Errorf("error getting the OpenShift version: %v", err)
return reconcile.Result{}, err

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

@ -12,7 +12,6 @@ import (
"github.com/golang/mock/gomock"
configv1 "github.com/openshift/api/config/v1"
configfake "github.com/openshift/client-go/config/clientset/versioned/fake"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
ctrlfake "sigs.k8s.io/controller-runtime/pkg/client/fake"
@ -94,10 +93,9 @@ func TestWorkaroundReconciler(t *testing.T) {
mwa := mock_workaround.NewMockWorkaround(controller)
r := &Reconciler{
configcli: configfake.NewSimpleClientset(clusterVersion("4.4.10")),
workarounds: []Workaround{mwa},
log: utillog.GetLogger(),
client: ctrlfake.NewClientBuilder().WithObjects(instance).Build(),
client: ctrlfake.NewClientBuilder().WithObjects(instance, clusterVersion("4.4.10")).Build(),
}
tt.mocker(mwa)
got, err := r.Reconcile(context.Background(), reconcile.Request{})

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

@ -4,20 +4,12 @@ package version
// Licensed under the Apache License 2.0.
import (
"context"
"errors"
configv1 "github.com/openshift/api/config/v1"
configclient "github.com/openshift/client-go/config/clientset/versioned"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func GetClusterVersion(ctx context.Context, configcli configclient.Interface) (*Version, error) {
cv, err := configcli.ConfigV1().ClusterVersions().Get(ctx, "version", metav1.GetOptions{})
if err != nil {
return nil, err
}
func GetClusterVersion(cv *configv1.ClusterVersion) (*Version, error) {
for _, history := range cv.Status.History {
if history.State == configv1.CompletedUpdate {
return ParseVersion(history.Version)
@ -26,12 +18,3 @@ func GetClusterVersion(ctx context.Context, configcli configclient.Interface) (*
return nil, errors.New("unknown cluster version")
}
func GetClusterDesiredVersion(ctx context.Context, configcli configclient.Interface) (*Version, error) {
cv, err := configcli.ConfigV1().ClusterVersions().Get(ctx, "version", metav1.GetOptions{})
if err != nil {
return nil, err
}
return ParseVersion(cv.Status.Desired.Version)
}