Skip to content

Commit

Permalink
detect: add capeability to detect HyperShift platform
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Tal-or committed Sep 4, 2024
1 parent 26dd242 commit 1e83dc0
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
15 changes: 13 additions & 2 deletions pkg/deployer/platform/detect/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
30 changes: 29 additions & 1 deletion pkg/deployer/platform/detect/detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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
}
3 changes: 3 additions & 0 deletions pkg/deployer/platform/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
Unknown = Platform("Unknown")
Kubernetes = Platform("Kubernetes")
OpenShift = Platform("OpenShift")
HyperShift = Platform("HyperShift")
)

func (p Platform) String() string {
Expand All @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/deployer/platform/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ func TestRoudnTrip(t *testing.T) {
expected: OpenShift,
expectedOK: true,
},
{
name: "HyperShift",
expected: HyperShift,
expectedOK: true,
},
{
name: "foobar",
expected: Unknown,
Expand Down

0 comments on commit 1e83dc0

Please sign in to comment.