Skip to content

Commit 4a0ac29

Browse files
committed
feat: implement onboarding cluster management and update related commands
1 parent feda8dc commit 4a0ac29

File tree

8 files changed

+131
-13
lines changed

8 files changed

+131
-13
lines changed

api/constants.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package api
2+
3+
const (
4+
UsageOperatorDomain = "usage.services.openmcp.cloud"
5+
UsageOperatorPlatformServiceName = "provider." + UsageOperatorDomain
6+
)

cmd/usage-operator/app/app.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,15 @@ import (
55
"fmt"
66
"os"
77

8+
"github.com/openmcp-project/controller-utils/pkg/clusters"
89
"github.com/openmcp-project/controller-utils/pkg/logging"
10+
clustersv1alpha1 "github.com/openmcp-project/openmcp-operator/api/clusters/v1alpha1"
911
"github.com/spf13/cobra"
12+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
13+
"k8s.io/apimachinery/pkg/runtime"
14+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
15+
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
16+
"k8s.io/client-go/tools/clientcmd/api"
1017

1118
ctrl "sigs.k8s.io/controller-runtime"
1219
"sigs.k8s.io/yaml"
@@ -21,7 +28,9 @@ func NewUsageOperatorCommand(ctx context.Context) *cobra.Command {
2128
cmd.SetErr(os.Stderr)
2229

2330
so := &SharedOptions{
24-
RawSharedOptions: &RawSharedOptions{},
31+
RawSharedOptions: &RawSharedOptions{
32+
PlatformCluster: clusters.New("platform"),
33+
},
2534
}
2635

2736
so.AddPersistentFlags(cmd)
@@ -35,6 +44,8 @@ func NewUsageOperatorCommand(ctx context.Context) *cobra.Command {
3544
type RawSharedOptions struct {
3645
Environment string `json:"environment"`
3746
DryRun bool `json:"dry-run"`
47+
48+
PlatformCluster *clusters.Cluster `json:"platform-cluster"`
3849
}
3950

4051
type SharedOptions struct {
@@ -50,9 +61,25 @@ func (o *SharedOptions) AddPersistentFlags(cmd *cobra.Command) {
5061
// misc
5162
cmd.PersistentFlags().BoolVar(&o.DryRun, "dry-run", false, "If set, the command aborts after evaluation of the given flags.")
5263
cmd.PersistentFlags().StringVar(&o.Environment, "environment", "", "Environment name. Required. This is used to distinguish between different environments that are watching the same Onboarding cluster. Must be globally unique.")
64+
65+
o.PlatformCluster.RegisterSingleConfigPathFlag(cmd.PersistentFlags())
5366
}
5467

5568
func (o *SharedOptions) Complete() error {
69+
// platform cluster
70+
if err := o.PlatformCluster.InitializeRESTConfig(); err != nil {
71+
return fmt.Errorf("unable to initialize platform cluster rest config: %w", err)
72+
}
73+
platformScheme := runtime.NewScheme()
74+
utilruntime.Must(clientgoscheme.AddToScheme(platformScheme))
75+
utilruntime.Must(apiextensionsv1.AddToScheme(platformScheme))
76+
utilruntime.Must(clustersv1alpha1.AddToScheme(platformScheme))
77+
utilruntime.Must(api.AddToScheme(platformScheme))
78+
79+
if err := o.PlatformCluster.InitializeClient(platformScheme); err != nil {
80+
return fmt.Errorf("unable to initialize platform cluster client: %w", err)
81+
}
82+
5683
// build logger
5784
log, err := logging.GetLogger()
5885
if err != nil {
@@ -74,7 +101,9 @@ func (o *SharedOptions) PrintRaw(cmd *cobra.Command) {
74101
}
75102

76103
func (o *SharedOptions) PrintCompleted(cmd *cobra.Command) {
77-
raw := map[string]any{}
104+
raw := map[string]any{
105+
"platform-cluster": o.PlatformCluster.APIServerEndpoint(),
106+
}
78107
data, err := yaml.Marshal(raw)
79108
if err != nil {
80109
cmd.Println(fmt.Errorf("error marshalling completed shared options: %w", err).Error())

cmd/usage-operator/app/init.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/openmcp-project/controller-utils/pkg/clusters"
87
crdutil "github.com/openmcp-project/controller-utils/pkg/crds"
98
apiconst "github.com/openmcp-project/openmcp-operator/api/constants"
109
"github.com/openmcp-project/openmcp-operator/api/install"
1110
"github.com/spf13/cobra"
1211
"k8s.io/apimachinery/pkg/runtime"
1312

14-
ctrl "sigs.k8s.io/controller-runtime"
15-
1613
"github.com/openmcp-project/usage-operator/api/crds"
14+
"github.com/openmcp-project/usage-operator/internal/helper"
1715
)
1816

1917
func NewInitCommand(so *SharedOptions) *cobra.Command {
@@ -63,14 +61,16 @@ func (o *InitOptions) Run(ctx context.Context) error {
6361
// apply CRDs
6462
crdManager := crdutil.NewCRDManager(apiconst.ClusterLabel, crds.CRDs)
6563

66-
var cluster clusters.Cluster
67-
cluster.WithRESTConfig(ctrl.GetConfigOrDie())
64+
cluster, err := helper.GetOnboardingCluster(ctx, log, o.PlatformCluster.Client())
65+
if err != nil {
66+
return fmt.Errorf("error when getting onboarding cluster: %w", err)
67+
}
6868

6969
if err := cluster.InitializeClient(install.InstallCRDAPIs(runtime.NewScheme())); err != nil {
7070
return fmt.Errorf("error initializing client: %w", err)
7171
}
7272

73-
crdManager.AddCRDLabelToClusterMapping("onboarding", &cluster)
73+
crdManager.AddCRDLabelToClusterMapping("onboarding", cluster)
7474

7575
if err := crdManager.CreateOrUpdateCRDs(ctx, &log); err != nil {
7676
return fmt.Errorf("error creating/updating CRDs: %w", err)

cmd/usage-operator/app/run.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
usagev1 "github.com/openmcp-project/usage-operator/api/usage/v1"
2626

2727
"github.com/openmcp-project/usage-operator/internal/controller"
28+
"github.com/openmcp-project/usage-operator/internal/helper"
2829
"github.com/openmcp-project/usage-operator/internal/runnable"
2930
"github.com/openmcp-project/usage-operator/internal/usage"
3031
)
@@ -241,11 +242,16 @@ func (o *RunOptions) Run(ctx context.Context) error {
241242
setupLog = o.Log.WithName("setup")
242243
setupLog.Info("Environment", "value", o.Environment)
243244

245+
cluster, err := helper.GetOnboardingCluster(ctx, setupLog, o.PlatformCluster.Client())
246+
if err != nil {
247+
return fmt.Errorf("error when getting onboarding cluster: %w", err)
248+
}
249+
244250
webhookServer := webhook.NewServer(webhook.Options{
245251
TLSOpts: o.WebhookTLSOpts,
246252
})
247253

248-
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
254+
mgr, err := ctrl.NewManager(cluster.RESTConfig(), ctrl.Options{
249255
Scheme: scheme,
250256
Metrics: o.MetricsServerOptions,
251257
WebhookServer: webhookServer,

cmd/usage-operator/app/uninstall.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ import (
55
"errors"
66
"fmt"
77

8-
"github.com/openmcp-project/controller-utils/pkg/clusters"
98
"github.com/openmcp-project/controller-utils/pkg/resources"
109
"github.com/openmcp-project/openmcp-operator/api/install"
1110
"github.com/spf13/cobra"
1211
"k8s.io/apimachinery/pkg/runtime"
13-
ctrl "sigs.k8s.io/controller-runtime"
1412

1513
"github.com/openmcp-project/usage-operator/api/crds"
14+
"github.com/openmcp-project/usage-operator/internal/helper"
1615
)
1716

1817
func NewUninstallCommand(so *SharedOptions) *cobra.Command {
@@ -63,8 +62,10 @@ func (o *UninstallOptions) Run(ctx context.Context) error {
6362
return fmt.Errorf("error when getting crds: %w", err)
6463
}
6564

66-
var cluster clusters.Cluster
67-
cluster.WithRESTConfig(ctrl.GetConfigOrDie())
65+
cluster, err := helper.GetOnboardingCluster(ctx, log, o.PlatformCluster.Client())
66+
if err != nil {
67+
return fmt.Errorf("error when getting onboarding cluster: %w", err)
68+
}
6869

6970
if err := cluster.InitializeClient(install.InstallCRDAPIs(runtime.NewScheme())); err != nil {
7071
return fmt.Errorf("error initializing client: %w", err)

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ require (
5353
github.com/modern-go/reflect2 v1.0.2 // indirect
5454
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
5555
github.com/nxadm/tail v1.4.11 // indirect
56+
github.com/openmcp-project/openmcp-operator/lib v0.8.3 // indirect
5657
github.com/pkg/errors v0.9.1 // indirect
5758
github.com/prometheus/client_golang v1.22.0 // indirect
5859
github.com/prometheus/client_model v0.6.1 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ github.com/openmcp-project/mcp-operator/api v0.30.0 h1:DqnoapCqzhgs/ypDsuSf01ltt
104104
github.com/openmcp-project/mcp-operator/api v0.30.0/go.mod h1:E7EPXYrmY4IpYtl6UGkppD7s5UZ6cKFZwN4ncIme7TY=
105105
github.com/openmcp-project/openmcp-operator/api v0.8.3 h1:s1c9kwvHUAkHZfSybb83Biw8qyia9pF4r2zxbfSM3qI=
106106
github.com/openmcp-project/openmcp-operator/api v0.8.3/go.mod h1:AflvCe/S41tO3x2rq4p+JnWxqVNtuwMn4LDPEVE00LE=
107+
github.com/openmcp-project/openmcp-operator/lib v0.8.3 h1:2bb1zbP6Si7/fUfXT9M5B0xQnd7O4zGJXaUoe9pDmcA=
108+
github.com/openmcp-project/openmcp-operator/lib v0.8.3/go.mod h1:oydIXRZoNDxtI4DI/JBUB08UPzvfdaKLqHvC4S4HXHQ=
107109
github.com/openmcp-project/project-workspace-operator/api v0.13.1 h1:Qq0110MdydwBHug5YFcRtHiN2Wtt9qJ5PYMvns/FiVY=
108110
github.com/openmcp-project/project-workspace-operator/api v0.13.1/go.mod h1:XFBFDMYCNFCNV5LQQs1FdStJ03ztrC5Exazbt9lZkvk=
109111
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=

internal/helper/clusteraccess.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package helper
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"time"
8+
9+
"github.com/openmcp-project/controller-utils/pkg/clusters"
10+
"github.com/openmcp-project/controller-utils/pkg/logging"
11+
"sigs.k8s.io/controller-runtime/pkg/client"
12+
13+
clustersv1alpha1 "github.com/openmcp-project/openmcp-operator/api/clusters/v1alpha1"
14+
openmcpconstv1alpha1 "github.com/openmcp-project/openmcp-operator/api/constants"
15+
"github.com/openmcp-project/openmcp-operator/lib/clusteraccess"
16+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
17+
18+
"github.com/openmcp-project/usage-operator/api"
19+
usagev1 "github.com/openmcp-project/usage-operator/api/usage/v1"
20+
rbacv1 "k8s.io/api/rbac/v1"
21+
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
22+
23+
"k8s.io/apimachinery/pkg/runtime"
24+
)
25+
26+
func GetOnboardingCluster(ctx context.Context, log logging.Logger, client client.Client) (*clusters.Cluster, error) {
27+
onboardingScheme := runtime.NewScheme()
28+
utilruntime.Must(clientgoscheme.AddToScheme(onboardingScheme))
29+
utilruntime.Must(usagev1.AddToScheme(onboardingScheme))
30+
utilruntime.Must(clustersv1alpha1.AddToScheme(onboardingScheme))
31+
32+
providerSystemNamespace := os.Getenv(openmcpconstv1alpha1.EnvVariablePlatformClusterNamespace)
33+
if providerSystemNamespace == "" {
34+
log.Error(nil, fmt.Sprintf("environment variable %s is not set", openmcpconstv1alpha1.EnvVariablePlatformClusterNamespace))
35+
return nil, fmt.Errorf("environment variable %s is not set", openmcpconstv1alpha1.EnvVariablePlatformClusterNamespace)
36+
}
37+
38+
clusterAccessManager := clusteraccess.NewClusterAccessManager(client, api.UsageOperatorPlatformServiceName, providerSystemNamespace).
39+
WithLogger(&log).
40+
WithInterval(10 * time.Second).
41+
WithTimeout(30 * time.Minute)
42+
43+
// TODO: Put the correct policies in there
44+
onboardingCluster, err := clusterAccessManager.CreateAndWaitForCluster(ctx, "onboarding", clustersv1alpha1.PURPOSE_ONBOARDING,
45+
onboardingScheme, []clustersv1alpha1.PermissionsRequest{
46+
{
47+
Rules: []rbacv1.PolicyRule{
48+
{
49+
APIGroups: []string{"core.openmcp.cloud"},
50+
Resources: []string{"managedcontrolplanes", "managedcontrolplanes/status"},
51+
Verbs: []string{"get", "list"},
52+
},
53+
{
54+
APIGroups: []string{"apiextensions.k8s.io"},
55+
Resources: []string{"customresourcedefinitions"},
56+
Verbs: []string{"create", "delete"},
57+
},
58+
{
59+
APIGroups: []string{"usage.openmcp.cloud"},
60+
Resources: []string{"*"},
61+
Verbs: []string{"*"},
62+
},
63+
},
64+
},
65+
},
66+
)
67+
if err != nil {
68+
log.Error(err, "error creating/updating onboarding cluster")
69+
return nil, fmt.Errorf("error creating/updating onboarding cluster: %w", err)
70+
}
71+
72+
return onboardingCluster, nil
73+
}

0 commit comments

Comments
 (0)