From 1e83dc0a65b07583490e6ea1437c922c5cba4459 Mon Sep 17 00:00:00 2001 From: Talor Itzhak Date: Wed, 4 Sep 2024 16:03:25 +0300 Subject: [PATCH] detect: add capeability to detect HyperShift platform Added the capeability for the deployer to detect whether the targeted OpenShift platform is of HyperShift flavor. In future PR on NROP, we'll consume this change to allow the operator performs specific decision given the current platform. Signed-off-by: Talor Itzhak --- pkg/deployer/platform/detect/detect.go | 15 +++++++++-- pkg/deployer/platform/detect/detect_test.go | 30 ++++++++++++++++++++- pkg/deployer/platform/platform.go | 3 +++ pkg/deployer/platform/platform_test.go | 5 ++++ 4 files changed, 50 insertions(+), 3 deletions(-) diff --git a/pkg/deployer/platform/detect/detect.go b/pkg/deployer/platform/detect/detect.go index 4519e6ad..894a5b07 100644 --- a/pkg/deployer/platform/detect/detect.go +++ b/pkg/deployer/platform/detect/detect.go @@ -64,14 +64,18 @@ func Platform(ctx context.Context) (platform.Platform, error) { if err != nil { return platform.Unknown, err } - return PlatformFromLister(ctx, ocpCli.ConfigV1.ClusterVersions()) + return PlatformFromClients(ctx, ocpCli.ConfigV1.ClusterVersions(), ocpCli.ConfigV1.Infrastructures()) } type ClusterVersionsLister interface { List(ctx context.Context, opts metav1.ListOptions) (*ocpconfigv1.ClusterVersionList, error) } -func PlatformFromLister(ctx context.Context, cvLister ClusterVersionsLister) (platform.Platform, error) { +type InfrastructuresGetter interface { + Get(ctx context.Context, name string, options metav1.GetOptions) (*ocpconfigv1.Infrastructure, error) +} + +func PlatformFromClients(ctx context.Context, cvLister ClusterVersionsLister, infraGetter InfrastructuresGetter) (platform.Platform, error) { vers, err := cvLister.List(ctx, metav1.ListOptions{}) if err != nil { if errors.IsNotFound(err) { @@ -80,6 +84,13 @@ func PlatformFromLister(ctx context.Context, cvLister ClusterVersionsLister) (pl return platform.Unknown, err } if len(vers.Items) > 0 { + infra, err := infraGetter.Get(ctx, "cluster", metav1.GetOptions{}) + if err != nil { + return platform.Unknown, err + } + if infra.Status.ControlPlaneTopology == ocpconfigv1.ExternalTopologyMode { + return platform.HyperShift, nil + } return platform.OpenShift, nil } return platform.Kubernetes, nil diff --git a/pkg/deployer/platform/detect/detect_test.go b/pkg/deployer/platform/detect/detect_test.go index 5cbf424d..1669214a 100644 --- a/pkg/deployer/platform/detect/detect_test.go +++ b/pkg/deployer/platform/detect/detect_test.go @@ -34,6 +34,7 @@ func TestPlatformFromLister(t *testing.T) { type testCase struct { name string vers []ocpconfigv1.ClusterVersion + infra ocpconfigv1.Infrastructure err error expectedPlatform platform.Platform expectedError error @@ -64,6 +65,19 @@ func TestPlatformFromLister(t *testing.T) { }, expectedPlatform: platform.OpenShift, }, + { + name: "hypershift", + // hypershift is a flavor of openshift so it has a clusterversion object + vers: []ocpconfigv1.ClusterVersion{ + {}, // zero object is fine! We just need 1+ elements + }, + infra: ocpconfigv1.Infrastructure{ + Status: ocpconfigv1.InfrastructureStatus{ + ControlPlaneTopology: ocpconfigv1.ExternalTopologyMode, + }, + }, + expectedPlatform: platform.HyperShift, + }, } for _, tc := range testCases { @@ -72,7 +86,12 @@ func TestPlatformFromLister(t *testing.T) { vers: tc.vers, err: tc.err, } - got, err := PlatformFromLister(context.TODO(), cli) + infraCli := fakeGetter{ + infra: tc.infra, + err: tc.err, + } + + got, err := PlatformFromClients(context.TODO(), cli, infraCli) if err != tc.expectedError { t.Errorf("got error %v expected %v", err, tc.expectedError) } @@ -88,9 +107,18 @@ type fakeLister struct { err error } +type fakeGetter struct { + infra ocpconfigv1.Infrastructure + err error +} + func (fake fakeLister) List(ctx context.Context, opts metav1.ListOptions) (*ocpconfigv1.ClusterVersionList, error) { verList := ocpconfigv1.ClusterVersionList{ Items: fake.vers, } return &verList, fake.err } + +func (fake fakeGetter) Get(ctx context.Context, name string, opts metav1.GetOptions) (*ocpconfigv1.Infrastructure, error) { + return &fake.infra, fake.err +} diff --git a/pkg/deployer/platform/platform.go b/pkg/deployer/platform/platform.go index a0b69ccf..2ce73633 100644 --- a/pkg/deployer/platform/platform.go +++ b/pkg/deployer/platform/platform.go @@ -24,6 +24,7 @@ const ( Unknown = Platform("Unknown") Kubernetes = Platform("Kubernetes") OpenShift = Platform("OpenShift") + HyperShift = Platform("HyperShift") ) func (p Platform) String() string { @@ -37,6 +38,8 @@ func ParsePlatform(plat string) (Platform, bool) { return Kubernetes, true case "openshift": return OpenShift, true + case "hypershift": + return HyperShift, true default: return Unknown, false } diff --git a/pkg/deployer/platform/platform_test.go b/pkg/deployer/platform/platform_test.go index a7918b04..101c07e1 100644 --- a/pkg/deployer/platform/platform_test.go +++ b/pkg/deployer/platform/platform_test.go @@ -36,6 +36,11 @@ func TestRoudnTrip(t *testing.T) { expected: OpenShift, expectedOK: true, }, + { + name: "HyperShift", + expected: HyperShift, + expectedOK: true, + }, { name: "foobar", expected: Unknown,