Skip to content

Commit 3feccf1

Browse files
Merge pull request #72 from abhinavdahiya/fix-apiserver-url
operator: change source of apiURL to infrastructure.config.openshift.io
2 parents bc0ccf6 + 578b7cc commit 3feccf1

File tree

3 files changed

+28
-73
lines changed

3 files changed

+28
-73
lines changed

pkg/operator/operator.go

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,93 +5,84 @@ import (
55
"encoding/json"
66
"fmt"
77

8-
corev1 "k8s.io/api/core/v1"
98
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
109
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
11-
"k8s.io/apimachinery/pkg/util/yaml"
1210
"k8s.io/client-go/dynamic"
13-
"k8s.io/client-go/informers/core/v1"
14-
coreclientv1 "k8s.io/client-go/kubernetes/typed/core/v1"
1511

1612
"github.com/openshift/cluster-authentication-operator/pkg/boilerplate/controller"
1713
"github.com/openshift/cluster-authentication-operator/pkg/boilerplate/operator"
1814
"github.com/openshift/library-go/pkg/operator/resource/resourcemerge"
1915
)
2016

2117
const (
22-
targetNamespaceName = "kube-system"
23-
targetConfigMap = "cluster-config-v1"
2418
oldTargetKubeAPIServerOperatorConfig = "instance"
2519
targetKubeAPIServerOperatorConfig = "cluster"
20+
targetInfratructureConfig = "cluster"
2621
)
2722

2823
type osinOperator struct {
29-
configMap coreclientv1.ConfigMapsGetter
3024
oldKubeAPIServerOperatorClient dynamic.ResourceInterface
3125
kubeAPIServerOperatorClient dynamic.ResourceInterface
26+
infrastructureConfigClient dynamic.ResourceInterface
3227
}
3328

34-
func NewOsinOperator(cmi v1.ConfigMapInformer, cm coreclientv1.ConfigMapsGetter,
29+
func NewOsinOperator(
3530
oldOperatorConfigInformer controller.InformerGetter, oldKubeAPIServerOperatorClient dynamic.ResourceInterface,
36-
kubeAPIServerOperatorConfigInformer controller.InformerGetter, kubeAPIServerOperatorClient dynamic.ResourceInterface) operator.Runner {
31+
kubeAPIServerOperatorConfigInformer controller.InformerGetter, kubeAPIServerOperatorClient dynamic.ResourceInterface,
32+
infrastructureConfigInformer controller.InformerGetter, infrastructureConfigClient dynamic.ResourceInterface) operator.Runner {
3733
c := &osinOperator{
38-
configMap: cm,
3934
oldKubeAPIServerOperatorClient: oldKubeAPIServerOperatorClient,
4035
kubeAPIServerOperatorClient: kubeAPIServerOperatorClient,
36+
infrastructureConfigClient: infrastructureConfigClient,
4137
}
4238

4339
return operator.New("OsinOperator", c,
44-
operator.WithInformer(cmi, operator.FilterByNames(targetConfigMap)),
4540
operator.WithInformer(oldOperatorConfigInformer, operator.FilterByNames(oldTargetKubeAPIServerOperatorConfig, targetKubeAPIServerOperatorConfig), controller.WithNoSync()),
4641
operator.WithInformer(kubeAPIServerOperatorConfigInformer, operator.FilterByNames(oldTargetKubeAPIServerOperatorConfig, targetKubeAPIServerOperatorConfig), controller.WithNoSync()),
42+
operator.WithInformer(infrastructureConfigInformer, operator.FilterByNames(targetInfratructureConfig), controller.WithNoSync()),
4743
)
4844
}
4945

5046
func (c osinOperator) Key() (metav1.Object, error) {
51-
return c.configMap.ConfigMaps(targetNamespaceName).Get(targetConfigMap, metav1.GetOptions{})
47+
return c.infrastructureConfigClient.Get(targetInfratructureConfig, metav1.GetOptions{})
5248
}
5349

5450
func (c osinOperator) Sync(obj metav1.Object) error {
55-
configMap := obj.(*corev1.ConfigMap)
56-
57-
installConfig := configMap.Data["install-config"]
58-
if len(installConfig) == 0 {
59-
return fmt.Errorf("no data: %#v", configMap)
60-
}
61-
installConfigJSON, err := yaml.ToJSON([]byte(installConfig))
51+
infra := obj.(*unstructured.Unstructured)
52+
// https://github.com/openshift/api/blob/ea5d05408a95a765d44b5a4b31561b530f0b1f4c/config/v1/types_infrastructure.go#L47
53+
apiURL, ok, err := unstructured.NestedString(infra.Object, "status", "apiServerURL")
6254
if err != nil {
6355
return err
6456
}
65-
ic := &InstallConfig{}
66-
if err := json.Unmarshal(installConfigJSON, ic); err != nil {
67-
return err
57+
if !ok || apiURL == "" {
58+
return fmt.Errorf("apiServerURL field not found")
6859
}
6960

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

76-
updateErr = updateKubeAPIServer(c.kubeAPIServerOperatorClient, oldTargetKubeAPIServerOperatorConfig, ic)
67+
updateErr = updateKubeAPIServer(c.kubeAPIServerOperatorClient, oldTargetKubeAPIServerOperatorConfig, apiURL)
7768
if updateErr == nil {
7869
return nil
7970
}
8071

81-
updateErr = updateKubeAPIServer(c.oldKubeAPIServerOperatorClient, targetKubeAPIServerOperatorConfig, ic)
72+
updateErr = updateKubeAPIServer(c.oldKubeAPIServerOperatorClient, targetKubeAPIServerOperatorConfig, apiURL)
8273
if updateErr == nil {
8374
return nil
8475
}
8576

86-
updateErr = updateKubeAPIServer(c.kubeAPIServerOperatorClient, targetKubeAPIServerOperatorConfig, ic)
77+
updateErr = updateKubeAPIServer(c.kubeAPIServerOperatorClient, targetKubeAPIServerOperatorConfig, apiURL)
8778
if updateErr == nil {
8879
return nil
8980
}
9081

9182
return updateErr
9283
}
9384

94-
func updateKubeAPIServer(kubeAPIServerOperatorClient dynamic.ResourceInterface, name string, ic *InstallConfig) error {
85+
func updateKubeAPIServer(kubeAPIServerOperatorClient dynamic.ResourceInterface, name, apiURL string) error {
9586
apiServerOperatorConfig, err := kubeAPIServerOperatorClient.Get(name, metav1.GetOptions{})
9687
if err != nil {
9788
return err
@@ -101,7 +92,6 @@ func updateKubeAPIServer(kubeAPIServerOperatorClient dynamic.ResourceInterface,
10192
return err
10293
}
10394

104-
apiURL := getAPIServerURL(ic)
10595
expectedOAuthConfig := map[string]interface{}{
10696
"spec": map[string]interface{}{
10797
"unsupportedConfigOverrides": map[string]interface{}{

pkg/operator/starter.go

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@ import (
66

77
"k8s.io/apimachinery/pkg/apis/meta/v1"
88
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
9-
"k8s.io/apimachinery/pkg/fields"
109
"k8s.io/apimachinery/pkg/runtime"
1110
"k8s.io/apimachinery/pkg/runtime/schema"
1211
"k8s.io/apimachinery/pkg/watch"
1312
"k8s.io/client-go/dynamic"
14-
"k8s.io/client-go/informers"
15-
"k8s.io/client-go/informers/internalinterfaces"
16-
"k8s.io/client-go/kubernetes"
1713
"k8s.io/client-go/tools/cache"
1814

1915
"github.com/openshift/cluster-authentication-operator/pkg/boilerplate/controller"
@@ -32,40 +28,37 @@ var kubeAPIServerOperatorConfigGVR = schema.GroupVersionResource{
3228
Version: "v1",
3329
Resource: "kubeapiservers",
3430
}
31+
var infrastructureConfigGVR = schema.GroupVersionResource{
32+
Group: "config.openshift.io",
33+
Version: "v1",
34+
Resource: "infrastructures",
35+
}
3536

3637
func RunOperator(ctx *controllercmd.ControllerContext) error {
37-
kubeClient, err := kubernetes.NewForConfig(ctx.KubeConfig)
38-
if err != nil {
39-
return err
40-
}
41-
4238
dynamicClient, err := dynamic.NewForConfig(ctx.KubeConfig)
4339
if err != nil {
4440
return err
4541
}
4642

47-
kubeInformersNamespaced := informers.NewSharedInformerFactoryWithOptions(kubeClient, resync,
48-
informers.WithNamespace(targetNamespaceName),
49-
informers.WithTweakListOptions(singleNameListOptions(targetConfigMap)),
50-
)
51-
5243
oldKubeAPIServerOperatorConfig := dynamicClient.Resource(oldKubeAPIServerOperatorConfigGVR)
5344
oldKubeAPIServerOperatorConfigInformer := dynamicInformer(oldKubeAPIServerOperatorConfig)
5445
kubeAPIServerOperatorConfig := dynamicClient.Resource(kubeAPIServerOperatorConfigGVR)
5546
kubeAPIServerOperatorConfigInformer := dynamicInformer(kubeAPIServerOperatorConfig)
47+
infrastructureConfig := dynamicClient.Resource(infrastructureConfigGVR)
48+
infrastructureConfigInformer := dynamicInformer(infrastructureConfig)
5649

5750
operator := NewOsinOperator(
58-
kubeInformersNamespaced.Core().V1().ConfigMaps(),
59-
kubeClient.CoreV1(),
6051
oldKubeAPIServerOperatorConfigInformer,
6152
oldKubeAPIServerOperatorConfig,
6253
kubeAPIServerOperatorConfigInformer,
6354
kubeAPIServerOperatorConfig,
55+
infrastructureConfigInformer,
56+
infrastructureConfig,
6457
)
6558

66-
kubeInformersNamespaced.Start(ctx.Context.Done())
6759
go oldKubeAPIServerOperatorConfigInformer.Informer().Run(ctx.Context.Done())
6860
go kubeAPIServerOperatorConfigInformer.Informer().Run(ctx.Context.Done())
61+
go infrastructureConfigInformer.Informer().Run(ctx.Context.Done())
6962

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

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

77-
func singleNameListOptions(name string) internalinterfaces.TweakListOptionsFunc {
78-
return func(opts *v1.ListOptions) {
79-
opts.FieldSelector = fields.OneTermEqualSelector("metadata.name", name).String()
80-
}
81-
}
82-
8370
func dynamicInformer(resource dynamic.ResourceInterface) controller.InformerGetter {
8471
lw := &cache.ListWatch{
8572
ListFunc: func(opts v1.ListOptions) (runtime.Object, error) {

pkg/operator/types.go

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)