Skip to content

operator: change source of apiURL to infrastructure.config.openshift.io #72

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

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
46 changes: 18 additions & 28 deletions pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,93 +5,84 @@ import (
"encoding/json"
"fmt"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/informers/core/v1"
coreclientv1 "k8s.io/client-go/kubernetes/typed/core/v1"

"github.com/openshift/cluster-authentication-operator/pkg/boilerplate/controller"
"github.com/openshift/cluster-authentication-operator/pkg/boilerplate/operator"
"github.com/openshift/library-go/pkg/operator/resource/resourcemerge"
)

const (
targetNamespaceName = "kube-system"
targetConfigMap = "cluster-config-v1"
oldTargetKubeAPIServerOperatorConfig = "instance"
targetKubeAPIServerOperatorConfig = "cluster"
targetInfratructureConfig = "cluster"
)

type osinOperator struct {
configMap coreclientv1.ConfigMapsGetter
oldKubeAPIServerOperatorClient dynamic.ResourceInterface
kubeAPIServerOperatorClient dynamic.ResourceInterface
infrastructureConfigClient dynamic.ResourceInterface
}

func NewOsinOperator(cmi v1.ConfigMapInformer, cm coreclientv1.ConfigMapsGetter,
func NewOsinOperator(
oldOperatorConfigInformer controller.InformerGetter, oldKubeAPIServerOperatorClient dynamic.ResourceInterface,
kubeAPIServerOperatorConfigInformer controller.InformerGetter, kubeAPIServerOperatorClient dynamic.ResourceInterface) operator.Runner {
kubeAPIServerOperatorConfigInformer controller.InformerGetter, kubeAPIServerOperatorClient dynamic.ResourceInterface,
infrastructureConfigInformer controller.InformerGetter, infrastructureConfigClient dynamic.ResourceInterface) operator.Runner {
c := &osinOperator{
configMap: cm,
oldKubeAPIServerOperatorClient: oldKubeAPIServerOperatorClient,
kubeAPIServerOperatorClient: kubeAPIServerOperatorClient,
infrastructureConfigClient: infrastructureConfigClient,
}

return operator.New("OsinOperator", c,
operator.WithInformer(cmi, operator.FilterByNames(targetConfigMap)),
operator.WithInformer(oldOperatorConfigInformer, operator.FilterByNames(oldTargetKubeAPIServerOperatorConfig, targetKubeAPIServerOperatorConfig), controller.WithNoSync()),
operator.WithInformer(kubeAPIServerOperatorConfigInformer, operator.FilterByNames(oldTargetKubeAPIServerOperatorConfig, targetKubeAPIServerOperatorConfig), controller.WithNoSync()),
operator.WithInformer(infrastructureConfigInformer, operator.FilterByNames(targetInfratructureConfig), controller.WithNoSync()),
)
}

func (c osinOperator) Key() (metav1.Object, error) {
return c.configMap.ConfigMaps(targetNamespaceName).Get(targetConfigMap, metav1.GetOptions{})
return c.infrastructureConfigClient.Get(targetInfratructureConfig, metav1.GetOptions{})
}

func (c osinOperator) Sync(obj metav1.Object) error {
configMap := obj.(*corev1.ConfigMap)

installConfig := configMap.Data["install-config"]
if len(installConfig) == 0 {
return fmt.Errorf("no data: %#v", configMap)
}
installConfigJSON, err := yaml.ToJSON([]byte(installConfig))
infra := obj.(*unstructured.Unstructured)
// https://github.com/openshift/api/blob/ea5d05408a95a765d44b5a4b31561b530f0b1f4c/config/v1/types_infrastructure.go#L47
apiURL, ok, err := unstructured.NestedString(infra.Object, "status", "apiServerURL")
if err != nil {
return err
}
ic := &InstallConfig{}
if err := json.Unmarshal(installConfigJSON, ic); err != nil {
return err
if !ok || apiURL == "" {
return fmt.Errorf("apiServerURL field not found")
}

// try all the potential names and resources to update. Eventually we'll be done with the old
updateErr := updateKubeAPIServer(c.oldKubeAPIServerOperatorClient, oldTargetKubeAPIServerOperatorConfig, ic)
updateErr := updateKubeAPIServer(c.oldKubeAPIServerOperatorClient, oldTargetKubeAPIServerOperatorConfig, apiURL)
if updateErr == nil {
return nil
}

updateErr = updateKubeAPIServer(c.kubeAPIServerOperatorClient, oldTargetKubeAPIServerOperatorConfig, ic)
updateErr = updateKubeAPIServer(c.kubeAPIServerOperatorClient, oldTargetKubeAPIServerOperatorConfig, apiURL)
if updateErr == nil {
return nil
}

updateErr = updateKubeAPIServer(c.oldKubeAPIServerOperatorClient, targetKubeAPIServerOperatorConfig, ic)
updateErr = updateKubeAPIServer(c.oldKubeAPIServerOperatorClient, targetKubeAPIServerOperatorConfig, apiURL)
if updateErr == nil {
return nil
}

updateErr = updateKubeAPIServer(c.kubeAPIServerOperatorClient, targetKubeAPIServerOperatorConfig, ic)
updateErr = updateKubeAPIServer(c.kubeAPIServerOperatorClient, targetKubeAPIServerOperatorConfig, apiURL)
if updateErr == nil {
return nil
}

return updateErr
}

func updateKubeAPIServer(kubeAPIServerOperatorClient dynamic.ResourceInterface, name string, ic *InstallConfig) error {
func updateKubeAPIServer(kubeAPIServerOperatorClient dynamic.ResourceInterface, name, apiURL string) error {
apiServerOperatorConfig, err := kubeAPIServerOperatorClient.Get(name, metav1.GetOptions{})
if err != nil {
return err
Expand All @@ -101,7 +92,6 @@ func updateKubeAPIServer(kubeAPIServerOperatorClient dynamic.ResourceInterface,
return err
}

apiURL := getAPIServerURL(ic)
expectedOAuthConfig := map[string]interface{}{
"spec": map[string]interface{}{
"unsupportedConfigOverrides": map[string]interface{}{
Expand Down
33 changes: 10 additions & 23 deletions pkg/operator/starter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@ import (

"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/informers"
"k8s.io/client-go/informers/internalinterfaces"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"

"github.com/openshift/cluster-authentication-operator/pkg/boilerplate/controller"
Expand All @@ -32,40 +28,37 @@ var kubeAPIServerOperatorConfigGVR = schema.GroupVersionResource{
Version: "v1",
Resource: "kubeapiservers",
}
var infrastructureConfigGVR = schema.GroupVersionResource{
Group: "config.openshift.io",
Version: "v1",
Resource: "infrastructures",
}

func RunOperator(ctx *controllercmd.ControllerContext) error {
kubeClient, err := kubernetes.NewForConfig(ctx.KubeConfig)
if err != nil {
return err
}

dynamicClient, err := dynamic.NewForConfig(ctx.KubeConfig)
if err != nil {
return err
}

kubeInformersNamespaced := informers.NewSharedInformerFactoryWithOptions(kubeClient, resync,
informers.WithNamespace(targetNamespaceName),
informers.WithTweakListOptions(singleNameListOptions(targetConfigMap)),
)

oldKubeAPIServerOperatorConfig := dynamicClient.Resource(oldKubeAPIServerOperatorConfigGVR)
oldKubeAPIServerOperatorConfigInformer := dynamicInformer(oldKubeAPIServerOperatorConfig)
kubeAPIServerOperatorConfig := dynamicClient.Resource(kubeAPIServerOperatorConfigGVR)
kubeAPIServerOperatorConfigInformer := dynamicInformer(kubeAPIServerOperatorConfig)
infrastructureConfig := dynamicClient.Resource(infrastructureConfigGVR)
infrastructureConfigInformer := dynamicInformer(infrastructureConfig)

operator := NewOsinOperator(
kubeInformersNamespaced.Core().V1().ConfigMaps(),
kubeClient.CoreV1(),
oldKubeAPIServerOperatorConfigInformer,
oldKubeAPIServerOperatorConfig,
kubeAPIServerOperatorConfigInformer,
kubeAPIServerOperatorConfig,
infrastructureConfigInformer,
infrastructureConfig,
)

kubeInformersNamespaced.Start(ctx.Context.Done())
go oldKubeAPIServerOperatorConfigInformer.Informer().Run(ctx.Context.Done())
go kubeAPIServerOperatorConfigInformer.Informer().Run(ctx.Context.Done())
go infrastructureConfigInformer.Informer().Run(ctx.Context.Done())

go operator.Run(ctx.Context.Done())

Expand All @@ -74,12 +67,6 @@ func RunOperator(ctx *controllercmd.ControllerContext) error {
return fmt.Errorf("stopped")
}

func singleNameListOptions(name string) internalinterfaces.TweakListOptionsFunc {
return func(opts *v1.ListOptions) {
opts.FieldSelector = fields.OneTermEqualSelector("metadata.name", name).String()
}
}

func dynamicInformer(resource dynamic.ResourceInterface) controller.InformerGetter {
lw := &cache.ListWatch{
ListFunc: func(opts v1.ListOptions) (runtime.Object, error) {
Expand Down
22 changes: 0 additions & 22 deletions pkg/operator/types.go

This file was deleted.