Skip to content

Commit

Permalink
Stop failing when cluster is down (#2234)
Browse files Browse the repository at this point in the history
  • Loading branch information
pPrecel authored Nov 4, 2024
1 parent f1d9c1b commit cb1487e
Show file tree
Hide file tree
Showing 17 changed files with 239 additions and 81 deletions.
21 changes: 18 additions & 3 deletions internal/cmd/alpha/hana/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ func runCheck(config *hanaCheckConfig) clierror.Error {
}

func checkHanaInstance(config *hanaCheckConfig) clierror.Error {
instance, err := config.KubeClient.Btp().GetServiceInstance(config.Ctx, config.namespace, config.name)
client, clientErr := config.GetKubeClientWithClierr()
if clientErr != nil {
return clientErr
}

instance, err := client.Btp().GetServiceInstance(config.Ctx, config.namespace, config.name)
if err != nil {
return clierror.New(err.Error())
}
Expand All @@ -80,7 +85,12 @@ func checkHanaInstance(config *hanaCheckConfig) clierror.Error {
}

func checkHanaBinding(config *hanaCheckConfig) clierror.Error {
binding, err := config.KubeClient.Btp().GetServiceBinding(config.Ctx, config.namespace, config.name)
client, clientErr := config.GetKubeClientWithClierr()
if clientErr != nil {
return clientErr
}

binding, err := client.Btp().GetServiceBinding(config.Ctx, config.namespace, config.name)
if err != nil {
return clierror.New(err.Error())
}
Expand All @@ -89,8 +99,13 @@ func checkHanaBinding(config *hanaCheckConfig) clierror.Error {
}

func checkHanaBindingURL(config *hanaCheckConfig) clierror.Error {
client, clientErr := config.GetKubeClientWithClierr()
if clientErr != nil {
return clientErr
}

urlName := hanaBindingURLName(config.name)
binding, err := config.KubeClient.Btp().GetServiceBinding(config.Ctx, config.namespace, urlName)
binding, err := client.Btp().GetServiceBinding(config.Ctx, config.namespace, urlName)
if err != nil {
return clierror.New(err.Error())
}
Expand Down
7 changes: 6 additions & 1 deletion internal/cmd/alpha/hana/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ func printCredentials(config *hanaCredentialsConfig, credentials credentials) {
}

func getHanaCredentials(config *hanaCredentialsConfig) (credentials, clierror.Error) {
secret, err := config.KubeClient.Static().CoreV1().Secrets(config.namespace).Get(config.Ctx, config.name, metav1.GetOptions{})
client, clientErr := config.GetKubeClientWithClierr()
if clientErr != nil {
return credentials{}, clientErr
}

secret, err := client.Static().CoreV1().Secrets(config.namespace).Get(config.Ctx, config.name, metav1.GetOptions{})
if err != nil {
return handleGetHanaCredentialsError(err)
}
Expand Down
21 changes: 18 additions & 3 deletions internal/cmd/alpha/hana/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,37 @@ func runDelete(config *hanaDeleteConfig) clierror.Error {
}

func deleteHanaInstance(config *hanaDeleteConfig) clierror.Error {
err := config.KubeClient.Dynamic().Resource(btp.GVRServiceInstance).
client, clientErr := config.GetKubeClientWithClierr()
if clientErr != nil {
return clientErr
}

err := client.Dynamic().Resource(btp.GVRServiceInstance).
Namespace(config.namespace).
Delete(config.Ctx, config.name, metav1.DeleteOptions{})
return handleDeleteResponse(err, "Hana instance", config.namespace, config.name)
}

func deleteHanaBinding(config *hanaDeleteConfig) clierror.Error {
err := config.KubeClient.Dynamic().Resource(btp.GVRServiceBinding).
client, clientErr := config.GetKubeClientWithClierr()
if clientErr != nil {
return clientErr
}

err := client.Dynamic().Resource(btp.GVRServiceBinding).
Namespace(config.namespace).
Delete(config.Ctx, config.name, metav1.DeleteOptions{})
return handleDeleteResponse(err, "Hana binding", config.namespace, config.name)
}

func deleteHanaBindingURL(config *hanaDeleteConfig) clierror.Error {
client, clientErr := config.GetKubeClientWithClierr()
if clientErr != nil {
return clientErr
}

urlName := hanaBindingURLName(config.name)
err := config.KubeClient.Dynamic().Resource(btp.GVRServiceBinding).
err := client.Dynamic().Resource(btp.GVRServiceBinding).
Namespace(config.namespace).
Delete(config.Ctx, urlName, metav1.DeleteOptions{})
return handleDeleteResponse(err, "Hana URL binding", config.namespace, urlName)
Expand Down
55 changes: 45 additions & 10 deletions internal/cmd/alpha/hana/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,13 @@ func runMap(config *hanaCheckConfig) clierror.Error {
}

func createHanaAPIInstanceIfNeeded(config *hanaCheckConfig) clierror.Error {
client, clientErr := config.GetKubeClientWithClierr()
if clientErr != nil {
return clientErr
}

// check if instance exists, skip API instance creation if it does
instance, err := config.KubeClient.Btp().GetServiceInstance(config.Ctx, config.namespace, hanaBindingAPIName(config.name))
instance, err := client.Btp().GetServiceInstance(config.Ctx, config.namespace, hanaBindingAPIName(config.name))
if err == nil && instance != nil {
fmt.Printf("Hana API instance already exists (%s/%s)\n", config.namespace, hanaBindingAPIName(config.name))
return nil
Expand All @@ -72,8 +77,13 @@ func createHanaAPIInstanceIfNeeded(config *hanaCheckConfig) clierror.Error {
}

func createHanaAPIBindingIfNeeded(config *hanaCheckConfig) clierror.Error {
client, clientErr := config.GetKubeClientWithClierr()
if clientErr != nil {
return clientErr
}

//check if binding exists, skip API binding creation if it does
instance, err := config.KubeClient.Btp().GetServiceBinding(config.Ctx, config.namespace, hanaBindingAPIName(config.name))
instance, err := client.Btp().GetServiceBinding(config.Ctx, config.namespace, hanaBindingAPIName(config.name))
if err == nil && instance != nil {
fmt.Printf("Hana API instance already exists (%s/%s)\n", config.namespace, hanaBindingAPIName(config.name))
return nil
Expand All @@ -84,16 +94,26 @@ func createHanaAPIBindingIfNeeded(config *hanaCheckConfig) clierror.Error {
}

func createHanaAPIInstance(config *hanaCheckConfig) clierror.Error {
client, clientErr := config.GetKubeClientWithClierr()
if clientErr != nil {
return clientErr
}

instance := hanaAPIInstance(config)

err := config.KubeClient.Btp().CreateServiceInstance(config.Ctx, instance)
err := client.Btp().CreateServiceInstance(config.Ctx, instance)
return handleProvisionResponse(err, "Hana API instance", config.namespace, hanaBindingAPIName(config.name))
}

func createHanaAPIBinding(config *hanaCheckConfig) clierror.Error {
client, clientErr := config.GetKubeClientWithClierr()
if clientErr != nil {
return clientErr
}

binding := hanaAPIBinding(config)

err := config.KubeClient.Btp().CreateServiceBinding(config.Ctx, binding)
err := client.Btp().CreateServiceBinding(config.Ctx, binding)
return handleProvisionResponse(err, "Hana API binding", config.namespace, hanaBindingAPIName(config.name))
}

Expand Down Expand Up @@ -172,17 +192,27 @@ func createHanaInstanceMapping(config *hanaCheckConfig) clierror.Error {
}

func getClusterID(config *hanaCheckConfig) (string, clierror.Error) {
cm, err := config.KubeClient.Static().CoreV1().ConfigMaps("kyma-system").Get(config.Ctx, "sap-btp-operator-config", metav1.GetOptions{})
client, clientErr := config.GetKubeClientWithClierr()
if clientErr != nil {
return "", clientErr
}

cm, err := client.Static().CoreV1().ConfigMaps("kyma-system").Get(config.Ctx, "sap-btp-operator-config", metav1.GetOptions{})
if err != nil {
return "", clierror.Wrap(err, clierror.New("failed to get cluster ID"))
}
return cm.Data["CLUSTER_ID"], nil
}

func getHanaID(config *hanaCheckConfig) (string, clierror.Error) {
client, clientErr := config.GetKubeClientWithClierr()
if clientErr != nil {
return "", clientErr
}

// wait for until Hana instance is ready, for default setting it should take 5 minutes
fmt.Print("waiting for Hana instance to be ready... ")
instanceReadyCheck := config.KubeClient.Btp().IsInstanceReady(config.Ctx, config.namespace, config.name)
instanceReadyCheck := client.Btp().IsInstanceReady(config.Ctx, config.namespace, config.name)
err := wait.PollUntilContextTimeout(config.Ctx, 10*time.Second, config.timeout, true, instanceReadyCheck)
if err != nil {
fmt.Println("Failed")
Expand All @@ -192,7 +222,7 @@ func getHanaID(config *hanaCheckConfig) (string, clierror.Error) {
}
fmt.Println("done")

instance, err := config.KubeClient.Btp().GetServiceInstance(config.Ctx, config.namespace, config.name)
instance, err := client.Btp().GetServiceInstance(config.Ctx, config.namespace, config.name)
if err != nil {
return "", clierror.Wrap(err, clierror.New("failed to get Hana instance"))
}
Expand All @@ -201,8 +231,13 @@ func getHanaID(config *hanaCheckConfig) (string, clierror.Error) {
}

func readHanaAPISecret(config *hanaCheckConfig) (string, *auth.UAA, clierror.Error) {
client, clientErr := config.GetKubeClientWithClierr()
if clientErr != nil {
return "", nil, clientErr
}

fmt.Print("waiting for Hana API instance to be ready... ")
instanceReadyCheck := config.KubeClient.Btp().IsInstanceReady(config.Ctx, config.namespace, hanaBindingAPIName(config.name))
instanceReadyCheck := client.Btp().IsInstanceReady(config.Ctx, config.namespace, hanaBindingAPIName(config.name))
err := wait.PollUntilContextTimeout(config.Ctx, 5*time.Second, 2*time.Minute, true, instanceReadyCheck)
if err != nil {
fmt.Println("Failed")
Expand All @@ -213,14 +248,14 @@ func readHanaAPISecret(config *hanaCheckConfig) (string, *auth.UAA, clierror.Err
fmt.Println("done")

fmt.Print("waiting for Hana API binding to be ready... ")
bindingReadyCheck := config.KubeClient.Btp().IsBindingReady(config.Ctx, config.namespace, hanaBindingAPIName(config.name))
bindingReadyCheck := client.Btp().IsBindingReady(config.Ctx, config.namespace, hanaBindingAPIName(config.name))
err = wait.PollUntilContextTimeout(config.Ctx, 5*time.Second, 2*time.Minute, true, bindingReadyCheck)
if err != nil {
fmt.Println("Failed")
return "", nil, clierror.Wrap(err, clierror.New("timeout while waiting for Hana API binding"))
}
fmt.Println("done")
secret, err := config.KubeClient.Static().CoreV1().Secrets(config.namespace).Get(config.Ctx, hanaBindingAPIName(config.name), metav1.GetOptions{})
secret, err := client.Static().CoreV1().Secrets(config.namespace).Get(config.Ctx, hanaBindingAPIName(config.name), metav1.GetOptions{})
if err != nil {
return "", nil, clierror.Wrap(err, clierror.New("failed to get secret"))
}
Expand Down
21 changes: 18 additions & 3 deletions internal/cmd/alpha/hana/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,36 @@ func runProvision(config *hanaProvisionConfig) clierror.Error {
func createHanaInstance(config *hanaProvisionConfig) clierror.Error {
instance := hanaInstance(config)

err := config.KubeClient.Btp().CreateServiceInstance(config.Ctx, instance)
client, clientErr := config.GetKubeClientWithClierr()
if clientErr != nil {
return clientErr
}

err := client.Btp().CreateServiceInstance(config.Ctx, instance)
return handleProvisionResponse(err, "Hana instance", config.namespace, config.name)
}

func createHanaBinding(config *hanaProvisionConfig) clierror.Error {
binding := hanaBinding(config)

err := config.KubeClient.Btp().CreateServiceBinding(config.Ctx, binding)
client, clientErr := config.GetKubeClientWithClierr()
if clientErr != nil {
return clientErr
}

err := client.Btp().CreateServiceBinding(config.Ctx, binding)
return handleProvisionResponse(err, "Hana binding", config.namespace, config.name)
}

func createHanaBindingURL(config *hanaProvisionConfig) clierror.Error {
bindingURL := hanaBindingURL(config)

err := config.KubeClient.Btp().CreateServiceBinding(config.Ctx, bindingURL)
client, clientErr := config.GetKubeClientWithClierr()
if clientErr != nil {
return clientErr
}

err := client.Btp().CreateServiceBinding(config.Ctx, bindingURL)
return handleProvisionResponse(err, "Hana URL binding", config.namespace, hanaBindingURLName(config.name))
}

Expand Down
25 changes: 21 additions & 4 deletions internal/cmd/alpha/modules/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,18 @@ func collectiveView(cfg *modulesConfig) clierror.Error {
if err != nil {
return clierror.WrapE(err, clierror.New("failed to get all Kyma catalog"))
}
installedWith, err := communitymodules.InstalledModules(cfg.Ctx, cfg.KubeClient)

client, err := cfg.GetKubeClientWithClierr()
if err != nil {
return err
}

installedWith, err := communitymodules.InstalledModules(cfg.Ctx, client)
if err != nil {
return clierror.WrapE(err, clierror.New("failed to get installed Kyma modules"))
}
managedWith, err := communitymodules.ManagedModules(cfg.Ctx, cfg.KubeClient)

managedWith, err := communitymodules.ManagedModules(cfg.Ctx, client)
if err != nil {
return clierror.WrapE(err, clierror.New("failed to get managed Kyma modules"))
}
Expand All @@ -99,7 +106,12 @@ func collectiveView(cfg *modulesConfig) clierror.Error {

// listInstalledModules lists all installed modules
func listInstalledModules(cfg *modulesConfig) clierror.Error {
installed, err := communitymodules.InstalledModules(cfg.Ctx, cfg.KubeClient)
client, err := cfg.GetKubeClientWithClierr()
if err != nil {
return err
}

installed, err := communitymodules.InstalledModules(cfg.Ctx, client)
if err != nil {
return clierror.WrapE(err, clierror.New("failed to get installed Kyma modules"))
}
Expand All @@ -110,7 +122,12 @@ func listInstalledModules(cfg *modulesConfig) clierror.Error {

// listManagedModules lists all managed modules
func listManagedModules(cfg *modulesConfig) clierror.Error {
managed, err := communitymodules.ManagedModules(cfg.Ctx, cfg.KubeClient)
client, err := cfg.GetKubeClientWithClierr()
if err != nil {
return err
}

managed, err := communitymodules.ManagedModules(cfg.Ctx, client)
if err != nil {
return clierror.WrapE(err, clierror.New("failed to get managed Kyma modules"))
}
Expand Down
7 changes: 6 additions & 1 deletion internal/cmd/alpha/oidc/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@ func runOIDC(cfg *oidcConfig) clierror.Error {
return clierror.WrapE(clierr, clierror.New("failed to get kubeconfig from CIS"))
}
} else {
kubeconfig = cfg.KubeClient.APIConfig()
client, err := cfg.GetKubeClientWithClierr()
if err != nil {
return err
}

kubeconfig = client.APIConfig()
}

enrichedKubeconfig := createKubeconfig(kubeconfig, token)
Expand Down
7 changes: 6 additions & 1 deletion internal/cmd/alpha/referenceinstance/referenceinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ func NewReferenceInstanceCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
func runReferenceInstance(config referenceInstanceConfig) error {
requestData := fillRequestData(config)

return config.KubeClient.Btp().CreateServiceInstance(config.Ctx, &requestData)
client, err := config.GetKubeClient()
if err != nil {
return err
}

return client.Btp().CreateServiceInstance(config.Ctx, &requestData)
}

func fillRequestData(config referenceInstanceConfig) btp.ServiceInstance {
Expand Down
7 changes: 6 additions & 1 deletion internal/cmd/alpha/registry/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ func NewConfigCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
}

func runConfig(cfg *cfgConfig) clierror.Error {
registryConfig, err := registry.GetExternalConfig(cfg.Ctx, cfg.KubeClient)
client, err := cfg.GetKubeClientWithClierr()
if err != nil {
return err
}

registryConfig, err := registry.GetExternalConfig(cfg.Ctx, client)
if err != nil {
return clierror.WrapE(err, clierror.New("failed to load in-cluster registry configuration"))
}
Expand Down
9 changes: 7 additions & 2 deletions internal/cmd/alpha/registry/imageimport/imageimport.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ func (pc *provisionConfig) complete(args []string) {
}

func runImageImport(config *provisionConfig) clierror.Error {
client, err := config.GetKubeClientWithClierr()
if err != nil {
return err
}

// TODO: Add "serverless is not installed" error message
registryConfig, err := registry.GetInternalConfig(config.Ctx, config.KubeClient)
registryConfig, err := registry.GetInternalConfig(config.Ctx, client)
if err != nil {
return clierror.WrapE(err, clierror.New("failed to load in-cluster registry configuration"))
}
Expand All @@ -65,7 +70,7 @@ func runImageImport(config *provisionConfig) clierror.Error {
config.Ctx,
config.image,
registry.ImportOptions{
ClusterAPIRestConfig: config.KubeClient.RestConfig(),
ClusterAPIRestConfig: client.RestConfig(),
RegistryAuth: registry.NewBasicAuth(registryConfig.SecretData.Username, registryConfig.SecretData.Password),
RegistryPullHost: registryConfig.SecretData.PullRegAddr,
RegistryPodName: registryConfig.PodMeta.Name,
Expand Down
7 changes: 6 additions & 1 deletion internal/cmd/alpha/remove/managed/managed.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,10 @@ func NewManagedCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
}

func runRemoveManaged(config *managedConfig) error {
return config.KubeClient.Kyma().DisableModule(config.Ctx, config.module)
client, err := config.GetKubeClient()
if err != nil {
return err
}

return client.Kyma().DisableModule(config.Ctx, config.module)
}
Loading

0 comments on commit cb1487e

Please sign in to comment.