Skip to content

Commit

Permalink
burn it
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonBaeumer committed Nov 21, 2023
1 parent 7436e13 commit 1787747
Showing 1 changed file with 1 addition and 112 deletions.
113 changes: 1 addition & 112 deletions internal/dinosaur/pkg/clusters/ocm_provider.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package clusters

import (
"fmt"
"net/http"
"strings"

"github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/clusters/types"
"github.com/stackrox/acs-fleet-manager/pkg/client/ocm"
"net/http"

"github.com/golang/glog"
clustersmgmtv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/pkg/errors"
"github.com/stackrox/acs-fleet-manager/pkg/api"
Expand Down Expand Up @@ -166,64 +162,6 @@ func (o *OCMProvider) GetComputeNodes(clusterSpec *types.ClusterSpec) (*types.Co
}, nil
}

// InstallDinosaurOperator ...
func (o *OCMProvider) InstallDinosaurOperator(clusterSpec *types.ClusterSpec) (bool, error) {
return o.installAddon(clusterSpec, o.ocmConfig.CentralOperatorAddonID)
}

// InstallFleetshard ...
func (o *OCMProvider) InstallFleetshard(clusterSpec *types.ClusterSpec, params []types.Parameter) (bool, error) {
return o.installAddonWithParams(clusterSpec, o.ocmConfig.FleetshardAddonID, params)
}

func (o *OCMProvider) installAddon(clusterSpec *types.ClusterSpec, addonID string) (bool, error) {
clusterID := clusterSpec.InternalID
addonInstallation, err := o.ocmClient.GetAddon(clusterID, addonID)
if err != nil {
return false, errors.Wrapf(err, "failed to get addon %s for cluster %s", addonID, clusterSpec.InternalID)
}

// Addon needs to be installed if addonInstallation doesn't exist
if addonInstallation.ID() == "" {
addonInstallation, err = o.ocmClient.CreateAddon(clusterID, addonID)
if err != nil {
return false, errors.Wrapf(err, "failed to create addon %s for cluster %s", addonID, clusterSpec.InternalID)
}
}

// The cluster is ready when the state reports ready
if addonInstallation.State() == clustersmgmtv1.AddOnInstallationStateReady {
return true, nil
}

return false, nil
}

func (o *OCMProvider) installAddonWithParams(clusterSpec *types.ClusterSpec, addonID string, params []types.Parameter) (bool, error) {
addonInstallation, addonErr := o.ocmClient.GetAddon(clusterSpec.InternalID, addonID)
if addonErr != nil {
return false, errors.Wrapf(addonErr, "failed to get addon %s for cluster %s", addonID, clusterSpec.InternalID)
}

if addonInstallation != nil && addonInstallation.ID() == "" {
glog.V(5).Infof("No existing %s addon found, create a new one", addonID)
addonInstallation, addonErr = o.ocmClient.CreateAddonWithParams(clusterSpec.InternalID, addonID, params)
if addonErr != nil {
return false, errors.Wrapf(addonErr, "failed to create addon %s for cluster %s", addonID, clusterSpec.InternalID)
}
}

if addonInstallation != nil && addonInstallation.State() == clustersmgmtv1.AddOnInstallationStateReady {
addonInstallation, addonErr = o.ocmClient.UpdateAddonParameters(clusterSpec.InternalID, addonInstallation.ID(), params)
if addonErr != nil {
return false, errors.Wrapf(addonErr, "failed to update parameters for addon %s on cluster %s", addonInstallation.ID(), clusterSpec.InternalID)
}
return true, nil
}

return false, nil
}

// GetCloudProviders ...
func (o *OCMProvider) GetCloudProviders() (*types.CloudProviderInfoList, error) {
list := types.CloudProviderInfoList{}
Expand Down Expand Up @@ -282,52 +220,3 @@ func newOCMProvider(ocmClient ocm.ClusterManagementClient, clusterBuilder Cluste
ocmConfig: ocmConfig,
}
}

func (o *OCMProvider) addOpenIDIdentityProvider(clusterSpec *types.ClusterSpec, openIDIdpInfo types.OpenIDIdentityProviderInfo) (string, error) {
provider, buildErr := buildIdentityProvider(openIDIdpInfo)
if buildErr != nil {
return "", errors.WithStack(buildErr)
}
createdIdentityProvider, createIdentityProviderErr := o.ocmClient.CreateIdentityProvider(clusterSpec.InternalID, provider)
if createIdentityProviderErr != nil {
// check to see if identity provider with name 'Dinosaur_SRE' already exists, if so use it.
if strings.Contains(createIdentityProviderErr.Error(), ipdAlreadyCreatedErrorToCheck) {
identityProvidersList, identityProviderListErr := o.ocmClient.GetIdentityProviderList(clusterSpec.InternalID)
if identityProviderListErr != nil {
return "", errors.WithStack(identityProviderListErr)
}

for _, identityProvider := range identityProvidersList.Slice() {
if identityProvider.Name() == openIDIdpInfo.Name {
return identityProvider.ID(), nil
}
}
}
return "", errors.WithStack(createIdentityProviderErr)
}
return createdIdentityProvider.ID(), nil
}

func buildIdentityProvider(idpInfo types.OpenIDIdentityProviderInfo) (*clustersmgmtv1.IdentityProvider, error) {
openIdentityBuilder := clustersmgmtv1.NewOpenIDIdentityProvider().
ClientID(idpInfo.ClientID).
ClientSecret(idpInfo.ClientSecret).
Claims(clustersmgmtv1.NewOpenIDClaims().
Email("email").
PreferredUsername("preferred_username").
Name("last_name", "preferred_username")).
Issuer(idpInfo.Issuer)

identityProviderBuilder := clustersmgmtv1.NewIdentityProvider().
Type("OpenIDIdentityProvider").
MappingMethod(clustersmgmtv1.IdentityProviderMappingMethodClaim).
OpenID(openIdentityBuilder).
Name(idpInfo.Name)

identityProvider, idpBuildErr := identityProviderBuilder.Build()
if idpBuildErr != nil {
return nil, fmt.Errorf("building identity provider: %w", idpBuildErr)
}

return identityProvider, nil
}

0 comments on commit 1787747

Please sign in to comment.