Skip to content

Commit 8c84f40

Browse files
authored
Allow customizing the CLI download link (#3281)
* remove cmdcommon.stringinslice in favor of slices.Contains from the standard library Signed-off-by: Nahshon Unna-Tsameret <[email protected]> * simplfy hcoutil.GetOperatorNamespaceFromEnv() stop returning error from this function. Only check that the OPERATOR_NAMESPACE environment variable is populated on boot. then don't check it anymore, to save many redundant error handlings. Signed-off-by: Nahshon Unna-Tsameret <[email protected]> * Stop recalculate namespaced names The current code re-calculates the expected namespaced name to resolve reconcile request. But the calculation is based on data that is never changed. This commit moves all the reconcile request resolving logic to a seperate package, for better readability and maintenance. The namespaced names are only calulated once, at boot time. Signed-off-by: Nahshon Unna-Tsameret <[email protected]> * add new request type for Ingress Signed-off-by: Nahshon Unna-Tsameret <[email protected]> * Add the new pkg/domainname package This package is a goroutine-safe storage of the CLI download host name, to be shared between multiple controller. Signed-off-by: Nahshon Unna-Tsameret <[email protected]> * Add new controller to watch the cluster ingress The controller updates the ingress status with the virt-download new component, to allow the user to customize the CLI download URLs. If the cli download host is customized (or stop been customized) the controller triggers an event in the hyperconverged controller to force it to update the download URLs. Signed-off-by: Nahshon Unna-Tsameret <[email protected]> * Add new request type The controller can now been trigger by the ingress controller notifications. Signed-off-by: Nahshon Unna-Tsameret <[email protected]> * Register the new ingress cluster controller Also, creates a new event channel, and give it to the hyperconverged controller (the reader) and the ingress clustr controller (the writer). Signed-off-by: Nahshon Unna-Tsameret <[email protected]> * Allow customizing the CLI download URLs Signed-off-by: Nahshon Unna-Tsameret <[email protected]> * Remove the GetDomain() function from the ClusterInfo interface This is not needed anymore. Signed-off-by: Nahshon Unna-Tsameret <[email protected]> * add e2e test for customized DL link Signed-off-by: Nahshon Unna-Tsameret <[email protected]> * add missing permissions Signed-off-by: Nahshon Unna-Tsameret <[email protected]> --------- Signed-off-by: Nahshon Unna-Tsameret <[email protected]>
1 parent ead329d commit 8c84f40

File tree

27 files changed

+1698
-317
lines changed

27 files changed

+1698
-317
lines changed

cmd/cmdcommon/cmdcommon.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ import (
88
"net/http/pprof"
99
"os"
1010
"runtime"
11-
12-
apiruntime "k8s.io/apimachinery/pkg/runtime"
13-
"sigs.k8s.io/controller-runtime/pkg/manager"
14-
15-
hcoutil "github.com/kubevirt/hyperconverged-cluster-operator/pkg/util"
11+
"slices"
1612

1713
"github.com/go-logr/logr"
1814
"github.com/spf13/pflag"
15+
apiruntime "k8s.io/apimachinery/pkg/runtime"
1916
logf "sigs.k8s.io/controller-runtime/pkg/log"
2017
"sigs.k8s.io/controller-runtime/pkg/log/zap"
18+
"sigs.k8s.io/controller-runtime/pkg/manager"
19+
20+
hcoutil "github.com/kubevirt/hyperconverged-cluster-operator/pkg/util"
2121
)
2222

2323
// list of namespace allowed for HCO installations (for tests)
@@ -121,7 +121,7 @@ func (h HcCmdHelper) printVersion() {
121121

122122
func (h HcCmdHelper) checkNameSpace() {
123123
// Get the namespace that we should be deployed in.
124-
requiredNS, err := hcoutil.GetOperatorNamespaceFromEnv()
124+
requiredNS, err := getOperatorNamespaceFromEnv()
125125
h.ExitOnError(err, "Failed to get namespace from the environment")
126126

127127
// Get the namespace we are currently deployed in.
@@ -144,7 +144,7 @@ func (h HcCmdHelper) checkNameSpace() {
144144
communityHubNamespace,
145145
communityHubTargetNamespace,
146146
}
147-
if !StringInSlice(actualNS, nsAllowList) {
147+
if !slices.Contains(nsAllowList, actualNS) {
148148
err := fmt.Errorf("%s is running in different namespace than expected", h.Name)
149149
msg := fmt.Sprintf("Please re-deploy this %s into %v namespace", h.Name, requiredNS)
150150
h.ExitOnError(err, msg, "Expected.Namespace", requiredNS, "Deployed.Namespace", actualNS)
@@ -166,11 +166,11 @@ func updateFlagSet(flags ...*flag.FlagSet) {
166166
}
167167
}
168168

169-
func StringInSlice(a string, list []string) bool {
170-
for _, b := range list {
171-
if b == a {
172-
return true
173-
}
169+
func getOperatorNamespaceFromEnv() (string, error) {
170+
namespace := os.Getenv(hcoutil.OperatorNamespaceEnv)
171+
if len(namespace) == 0 {
172+
return "", fmt.Errorf("%s unset or empty in environment", hcoutil.OperatorNamespaceEnv)
174173
}
175-
return false
174+
175+
return namespace, nil
176176
}

cmd/hyperconverged-cluster-operator/main.go

+19-6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"sigs.k8s.io/controller-runtime/pkg/cache"
3636
"sigs.k8s.io/controller-runtime/pkg/client"
3737
"sigs.k8s.io/controller-runtime/pkg/client/config"
38+
"sigs.k8s.io/controller-runtime/pkg/event"
3839
"sigs.k8s.io/controller-runtime/pkg/healthz"
3940
logf "sigs.k8s.io/controller-runtime/pkg/log"
4041
"sigs.k8s.io/controller-runtime/pkg/manager"
@@ -54,6 +55,7 @@ import (
5455
"github.com/kubevirt/hyperconverged-cluster-operator/controllers/crd"
5556
"github.com/kubevirt/hyperconverged-cluster-operator/controllers/descheduler"
5657
"github.com/kubevirt/hyperconverged-cluster-operator/controllers/hyperconverged"
58+
"github.com/kubevirt/hyperconverged-cluster-operator/controllers/ingresscluster"
5759
"github.com/kubevirt/hyperconverged-cluster-operator/controllers/nodes"
5860
"github.com/kubevirt/hyperconverged-cluster-operator/controllers/observability"
5961
"github.com/kubevirt/hyperconverged-cluster-operator/controllers/operands"
@@ -98,8 +100,7 @@ var (
98100
func main() {
99101
cmdHelper.InitiateCommand()
100102

101-
operatorNamespace, err := hcoutil.GetOperatorNamespaceFromEnv()
102-
cmdHelper.ExitOnError(err, "can't get operator expected namespace")
103+
operatorNamespace := hcoutil.GetOperatorNamespaceFromEnv()
103104

104105
// Get a config to talk to the apiserver
105106
cfg, err := config.GetConfig()
@@ -176,17 +177,21 @@ func main() {
176177
upgradeableCondition, err = hcoutil.NewOperatorCondition(ci, mgr.GetClient(), operatorsapiv2.Upgradeable)
177178
cmdHelper.ExitOnError(err, "Cannot create Upgradeable Operator Condition")
178179

179-
// a channel to trigger a restart of the operator
180-
// via a clean cancel of the manager
181-
restartCh := make(chan struct{})
180+
ingressEventCh := make(chan event.TypedGenericEvent[client.Object], 10)
181+
defer close(ingressEventCh)
182182

183183
// Create a new reconciler
184-
if err := hyperconverged.RegisterReconciler(mgr, ci, upgradeableCondition); err != nil {
184+
if err := hyperconverged.RegisterReconciler(mgr, ci, upgradeableCondition, ingressEventCh); err != nil {
185185
logger.Error(err, "failed to register the HyperConverged controller")
186186
eventEmitter.EmitEvent(nil, corev1.EventTypeWarning, "InitError", "Unable to register HyperConverged controller; "+err.Error())
187187
os.Exit(1)
188188
}
189189

190+
// a channel to trigger a restart of the operator
191+
// via a clean cancel of the manager
192+
restartCh := make(chan struct{})
193+
defer close(restartCh)
194+
190195
// Create a new CRD reconciler
191196
if err := crd.RegisterReconciler(mgr, restartCh); err != nil {
192197
logger.Error(err, "failed to register the CRD controller")
@@ -217,6 +222,14 @@ func main() {
217222
os.Exit(1)
218223
}
219224

225+
if ci.IsOpenshift() {
226+
if err = ingresscluster.RegisterReconciler(mgr, ingressEventCh); err != nil {
227+
logger.Error(err, "failed to register the IngressCluster controller")
228+
eventEmitter.EmitEvent(nil, corev1.EventTypeWarning, "InitError", "Unable to register Ingress controller; "+err.Error())
229+
os.Exit(1)
230+
}
231+
}
232+
220233
err = createPriorityClass(ctx, mgr)
221234
cmdHelper.ExitOnError(err, "Failed creating PriorityClass")
222235

cmd/hyperconverged-cluster-webhook/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ func main() {
6666

6767
cmdHelper.InitiateCommand()
6868

69-
operatorNamespace, err := hcoutil.GetOperatorNamespaceFromEnv()
70-
cmdHelper.ExitOnError(err, "can't get operator expected namespace")
69+
operatorNamespace := hcoutil.GetOperatorNamespaceFromEnv()
7170

7271
// Get a config to talk to the apiserver
7372
cfg, err := config.GetConfig()

controllers/commontestutils/testUtils.go

-9
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,6 @@ func (ClusterInfoMock) IsInfrastructureHighlyAvailable() bool {
303303
func (ClusterInfoMock) SetHighAvailabilityMode(_ context.Context, _ client.Client) error {
304304
return nil
305305
}
306-
func (ClusterInfoMock) GetDomain() string {
307-
return "domain"
308-
}
309306
func (ClusterInfoMock) GetBaseDomain() string {
310307
return BaseDomain
311308
}
@@ -378,9 +375,6 @@ func (ClusterInfoSNOMock) IsInfrastructureHighlyAvailable() bool {
378375
func (ClusterInfoSNOMock) SetHighAvailabilityMode(_ context.Context, _ client.Client) error {
379376
return nil
380377
}
381-
func (ClusterInfoSNOMock) GetDomain() string {
382-
return "domain"
383-
}
384378
func (ClusterInfoSNOMock) GetBaseDomain() string {
385379
return BaseDomain
386380
}
@@ -464,9 +458,6 @@ func (ClusterInfoSRCPHAIMock) GetDeployment() *appsv1.Deployment {
464458
func (ClusterInfoSRCPHAIMock) GetCSV() *csvv1alpha1.ClusterServiceVersion {
465459
return csv
466460
}
467-
func (ClusterInfoSRCPHAIMock) GetDomain() string {
468-
return "domain"
469-
}
470461
func (ClusterInfoSRCPHAIMock) GetBaseDomain() string {
471462
return BaseDomain
472463
}

0 commit comments

Comments
 (0)