Skip to content

Commit 272f75f

Browse files
committed
read featuregates from the shared API value
1 parent d1d5d55 commit 272f75f

File tree

3 files changed

+53
-21
lines changed

3 files changed

+53
-21
lines changed

pkg/cmd/start/start.go

+41
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@ package start
22

33
import (
44
"context"
5+
"fmt"
6+
"github.com/openshift/library-go/pkg/operator/events"
57
"math/rand"
68
"os"
79
"time"
810

11+
v1 "github.com/openshift/api/config/v1"
12+
configv1client "github.com/openshift/client-go/config/clientset/versioned"
13+
configv1informers "github.com/openshift/client-go/config/informers/externalversions"
914
"github.com/openshift/library-go/pkg/controller/controllercmd"
15+
"github.com/openshift/library-go/pkg/operator/configobserver/featuregates"
1016
"github.com/openshift/library-go/pkg/serviceability"
1117

1218
"github.com/spf13/cobra"
@@ -113,6 +119,41 @@ func runGather(operator *controller.GatherJob, cfg *controllercmd.ControllerComm
113119
protoConfig.ContentType = "application/vnd.kubernetes.protobuf"
114120

115121
ctx, cancel := context.WithTimeout(context.Background(), operator.Interval)
122+
123+
// before calling the Gather logic, this command must check the featuregate status.
124+
configClient, err := configv1client.NewForConfig(clientConfig)
125+
if err != nil {
126+
klog.Exit("error: building client: %v", err)
127+
}
128+
configInformers := configv1informers.NewSharedInformerFactory(configClient, 10*time.Minute)
129+
130+
desiredVersion := os.Getenv("RELEASE_VERSION")
131+
missingVersion := "0.0.1-snapshot"
132+
133+
// By default, this will exit(0) the process if the featuregates ever change to a different set of values.
134+
featureGateAccessor := featuregates.NewFeatureGateAccess(
135+
desiredVersion, missingVersion,
136+
configInformers.Config().V1().ClusterVersions(), configInformers.Config().V1().FeatureGates(),
137+
events.NewLoggingEventRecorder("insights-gather"),
138+
)
139+
go featureGateAccessor.Run(ctx)
140+
go configInformers.Start(ctx.Done())
141+
142+
select {
143+
case <-featureGateAccessor.InitialFeatureGatesObserved():
144+
featureGates, _ := featureGateAccessor.CurrentFeatureGates()
145+
klog.Infof("FeatureGates initialized: knownFeatureGates=%v", featureGates.KnownFeatures())
146+
case <-time.After(1 * time.Minute):
147+
klog.Errorf("timed out waiting for FeatureGate detection")
148+
klog.Exit(fmt.Errorf("timed out waiting for FeatureGate detection"))
149+
}
150+
151+
featureGates, err := featureGateAccessor.CurrentFeatureGates()
152+
if err != nil {
153+
klog.Exit(err)
154+
}
155+
operator.InsightsConfigAPIEnabled = featureGates.Enabled(v1.FeatureGateInsightsConfigAPI)
156+
116157
err = operator.Gather(ctx, clientConfig, protoConfig)
117158
if err != nil {
118159
klog.Error(err)

pkg/controller/gather_job.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
// GatherJob is the type responsible for controlling a non-periodic Gather execution
2727
type GatherJob struct {
2828
config.Controller
29+
InsightsConfigAPIEnabled bool
2930
}
3031

3132
// Gather runs a single gather and stores the generated archive, without uploading it.
@@ -51,12 +52,8 @@ func (d *GatherJob) Gather(ctx context.Context, kubeConfig, protoKubeConfig *res
5152
protoKubeConfig, kubeConfig, d.Impersonate,
5253
)
5354

54-
tpEnabled, err := isTechPreviewEnabled(ctx, configClient)
55-
if err != nil {
56-
klog.Error("can't read cluster feature gates: %v", err)
57-
}
5855
var gatherConfig v1alpha1.GatherConfig
59-
if tpEnabled {
56+
if d.InsightsConfigAPIEnabled {
6057
insightsDataGather, err := configClient.ConfigV1alpha1().InsightsDataGathers().Get(ctx, "cluster", metav1.GetOptions{}) //nolint: govet
6158
if err != nil {
6259
return err

pkg/controller/operator.go

+10-16
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func (s *Operator) Run(ctx context.Context, controller *controllercmd.Controller
6666
if err != nil {
6767
return err
6868
}
69+
configInformers := configv1informers.NewSharedInformerFactory(configClient, 10*time.Minute)
6970

7071
operatorClient, err := operatorv1client.NewForConfig(controller.KubeConfig)
7172
if err != nil {
@@ -83,52 +84,45 @@ func (s *Operator) Run(ctx context.Context, controller *controllercmd.Controller
8384
return err
8485
}
8586

86-
desiredVersion := config.OperatorReleaseVersion
87+
desiredVersion := os.Getenv("RELEASE_VERSION")
8788
missingVersion := "0.0.1-snapshot"
8889

8990
// By default, this will exit(0) the process if the featuregates ever change to a different set of values.
9091
featureGateAccessor := featuregates.NewFeatureGateAccess(
9192
desiredVersion, missingVersion,
9293
configInformers.Config().V1().ClusterVersions(), configInformers.Config().V1().FeatureGates(),
93-
eventRecorder,
94+
controller.EventRecorder,
9495
)
9596
go featureGateAccessor.Run(ctx)
96-
go configInformers.Start(config.Stop)
97+
go configInformers.Start(ctx.Done())
9798

9899
select {
99100
case <-featureGateAccessor.InitialFeatureGatesObserved():
100101
featureGates, _ := featureGateAccessor.CurrentFeatureGates()
101-
log.Info("FeatureGates initialized", "knownFeatures", featureGates.KnownFeatures())
102+
klog.Infof("FeatureGates initialized: knownFeatureGates=%v", featureGates.KnownFeatures())
102103
case <-time.After(1 * time.Minute):
103-
log.Error(nil, "timed out waiting for FeatureGate detection")
104-
return nil, fmt.Errorf("timed out waiting for FeatureGate detection")
104+
klog.Errorf("timed out waiting for FeatureGate detection")
105+
return fmt.Errorf("timed out waiting for FeatureGate detection")
105106
}
106107

107108
featureGates, err := featureGateAccessor.CurrentFeatureGates()
108109
if err != nil {
109-
return nil, err
110+
return err
110111
}
111-
// example of future featuregate read and usage to set a variable to pass to a controller
112-
gatewayAPIEnabled := featureGates.Enabled(configv1.FeatureGateGatewayAPI)
112+
insightsConfigAPIEnabled := featureGates.Enabled(v1.FeatureGateInsightsConfigAPI)
113113

114114
// ensure the insight snapshot directory exists
115115
if _, err = os.Stat(s.StoragePath); err != nil && os.IsNotExist(err) {
116116
if err = os.MkdirAll(s.StoragePath, 0777); err != nil {
117117
return fmt.Errorf("can't create --path: %v", err)
118118
}
119119
}
120-
tpEnabled, err := isTechPreviewEnabled(ctx, configClient)
121-
if err != nil {
122-
klog.Error("can't read cluster feature gates: %v", err)
123-
}
124120
var apiConfigObserver configobserver.APIConfigObserver
125-
if tpEnabled {
126-
configInformers := configv1informers.NewSharedInformerFactory(configClient, 10*time.Minute)
121+
if insightsConfigAPIEnabled {
127122
apiConfigObserver, err = configobserver.NewAPIConfigObserver(gatherKubeConfig, controller.EventRecorder, configInformers)
128123
if err != nil {
129124
return err
130125
}
131-
configInformers.Start(ctx.Done())
132126
go apiConfigObserver.Run(ctx, 1)
133127
}
134128

0 commit comments

Comments
 (0)