Skip to content

Commit 5d37b30

Browse files
committed
fix: check if resource exists in update cmd
1 parent c6fae07 commit 5d37b30

File tree

5 files changed

+101
-48
lines changed

5 files changed

+101
-48
lines changed

internal/cli/cmd/install/install.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ func NewCmd() *cobra.Command {
2626
logger := utils.GetDefaultLoggerInstance()
2727
cmd := &cobra.Command{
2828
Use: "install <component>",
29-
Short: "Command for ob-operator and components installation",
30-
Long: `Command for ob-operator and components installation.
29+
Short: "Command for ob-operator and other components installation",
30+
Long: `Command for ob-operator and other components installation.
3131
3232
Currently support:
3333
- ob-operator: A Kubernetes operator that simplifies the deployment and management of OceanBase cluster and related resources on Kubernetes.
3434
- ob-dashboard: A web application that provides resource management capabilities.
3535
- local-path-provisioner: Provides a way for the Kubernetes users to utilize the local storage in each node, Storage of OceanBase cluster relies on it, which should be installed beforehand.
3636
- cert-manager: Creates TLS certificates for workloads in Kubernetes and renews the certificates before they expire, ob-operator relies on it for certificate management, which should be installed beforehand.
3737
38-
if not specified, install ob-operator and ob-dashboard by default`,
38+
if not specified, install ob-operator and ob-dashboard by default, and cert-manager if it is not found in cluster.`,
3939
PreRunE: o.Parse,
4040
Args: cobra.MaximumNArgs(1),
4141
Run: func(cmd *cobra.Command, args []string) {
@@ -46,7 +46,7 @@ if not specified, install ob-operator and ob-dashboard by default`,
4646
if err := o.Install(component, version); err != nil {
4747
logger.Fatalln(err)
4848
} else {
49-
logger.Printf("%s install successfully", component)
49+
logger.Printf("%s install successfully\n", component)
5050
}
5151
}
5252
},

internal/cli/cmd/update/update.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ func NewCmd() *cobra.Command {
2626
logger := utils.GetDefaultLoggerInstance()
2727
cmd := &cobra.Command{
2828
Use: "update <component>",
29-
Short: "Command for ob-operator and components update",
30-
Long: `Command for ob-operator and components update.
29+
Short: "Command for ob-operator and other components update",
30+
Long: `Command for ob-operator and other components update.
3131
3232
Currently support:
3333
- ob-operator: A Kubernetes operator that simplifies the deployment and management of OceanBase cluster and related resources on Kubernetes.
@@ -39,16 +39,22 @@ if not specified, update ob-operator and ob-dashboard by default`,
3939
PreRunE: o.Parse,
4040
Args: cobra.MaximumNArgs(1),
4141
Run: func(cmd *cobra.Command, args []string) {
42-
if len(args) == 0 {
43-
logger.Println("update ob-operator and ob-dashboard by default")
44-
}
42+
componentCount := 0
4543
for component, version := range o.Components {
46-
if err := o.Install(component, version); err != nil {
47-
logger.Fatalln(err)
44+
if utils.CheckIfComponentExists(component) {
45+
componentCount++
46+
if err := o.Install(component, version); err != nil {
47+
logger.Fatalln(err)
48+
} else {
49+
logger.Printf("%s update successfully\n", component)
50+
}
4851
} else {
49-
logger.Printf("%s update successfully", component)
52+
logger.Printf("Component %s is not found\n", component)
5053
}
5154
}
55+
if componentCount == 0 {
56+
logger.Println("No components to update")
57+
}
5258
},
5359
}
5460
return cmd

internal/cli/install/install.go

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ See the Mulan PSL v2 for more details.
1414
package install
1515

1616
import (
17-
"bytes"
1817
"fmt"
1918
"os"
2019
"os/exec"
@@ -46,7 +45,7 @@ func (o *InstallOptions) AddFlags(cmd *cobra.Command) {
4645
func (o *InstallOptions) Parse(_ *cobra.Command, args []string) error {
4746
// if not specified, use default config
4847
if len(args) == 0 {
49-
defaultComponents := o.getDefaultComponents()
48+
defaultComponents := o.GetDefaultComponents()
5049
// update Components to default config
5150
o.Components = defaultComponents
5251
return nil
@@ -104,39 +103,10 @@ func (o *InstallOptions) buildCmd(component, version string) (*exec.Cmd, error)
104103
return cmd, nil
105104
}
106105

107-
// checkCertManager checks cert-manager in the environment
108-
func checkCertManager() bool {
109-
cmd := exec.Command("kubectl", "get", "crds", "-o", "name", "|", "grep", "cert-manager")
110-
var out bytes.Buffer
111-
cmd.Stdout = &out
112-
err := cmd.Run()
113-
if err != nil {
114-
return false
115-
}
116-
117-
// Check if the output contains cert-manager resources
118-
expectedResources := []string{
119-
"challenges.acme.cert-manager.io",
120-
"orders.acme.cert-manager.io",
121-
"certificaterequests.cert-manager.io",
122-
"certificates.cert-manager.io",
123-
"clusterissuers.cert-manager.io",
124-
"issuers.cert-manager.io",
125-
}
126-
127-
for _, resource := range expectedResources {
128-
if !bytes.Contains(out.Bytes(), []byte(resource)) {
129-
return false
130-
}
131-
}
132-
133-
return true
134-
}
135-
136-
func (o *InstallOptions) getDefaultComponents() map[string]string {
106+
func (o *InstallOptions) GetDefaultComponents() map[string]string {
137107
defaultComponents := make(map[string]string) // Initialize the map
138108
var componentsList []string
139-
if !checkCertManager() {
109+
if !utils.CheckIfComponentExists("cert-manager") {
140110
componentsList = []string{"cert-manager", "ob-operator", "ob-dashboard"}
141111
} else {
142112
componentsList = []string{"ob-operator", "ob-dashboard"}

internal/cli/update/update.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,17 @@ func NewUpdateOptions() *UpdateOptions {
3232
}
3333

3434
func (o *UpdateOptions) Parse(_ *cobra.Command, args []string) error {
35+
// if not specified, use default config
3536
if len(args) == 0 {
37+
defaultComponents := o.GetDefaultComponents()
38+
// update Components to default config
39+
o.Components = defaultComponents
3640
return nil
3741
}
3842
name := args[0]
39-
if _, ok := o.Components[name]; !ok {
40-
return fmt.Errorf("%s update not supported", name)
43+
if v, ok := o.Components[name]; ok {
44+
o.Components = map[string]string{name: v}
45+
return nil
4146
}
42-
return nil
47+
return fmt.Errorf("component %s is not supported", name)
4348
}

internal/cli/utils/checker.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,51 @@ See the Mulan PSL v2 for more details.
1414
package utils
1515

1616
import (
17+
"bytes"
1718
"fmt"
19+
"os/exec"
1820

1921
apitypes "github.com/oceanbase/ob-operator/api/types"
2022
"github.com/oceanbase/ob-operator/api/v1alpha1"
2123
clusterstatus "github.com/oceanbase/ob-operator/internal/const/status/obcluster"
2224
"github.com/oceanbase/ob-operator/internal/const/status/tenantstatus"
2325
)
2426

27+
var (
28+
operatorCheckCmd = "kubectl get crds -A -o name | grep oceanbase.oceanbase.com"
29+
certManagerCheckCmd = "kubectl get crds -o name | grep cert-manager"
30+
dashboardCheckCmd = "helm list | grep oceanbase-dashboard"
31+
localPathProvisionerCheckCmd = "kubectl get deployment -A | grep local-path-provisioner"
32+
)
33+
34+
var (
35+
// Define the resources to check for each command
36+
certManagerResources = []string{
37+
"challenges.acme.cert-manager.io",
38+
"orders.acme.cert-manager.io",
39+
"certificaterequests.cert-manager.io",
40+
"certificates.cert-manager.io",
41+
"clusterissuers.cert-manager.io",
42+
"issuers.cert-manager.io",
43+
}
44+
45+
operatorResources = []string{
46+
"obparameters.oceanbase.oceanbase.com",
47+
"observers.oceanbase.oceanbase.com",
48+
"obclusters.oceanbase.oceanbase.com",
49+
"obtenantbackups.oceanbase.oceanbase.com",
50+
"obtenantrestores.oceanbase.oceanbase.com",
51+
"obzones.oceanbase.oceanbase.com",
52+
"obtenants.oceanbase.oceanbase.com",
53+
"obtenantoperations.oceanbase.oceanbase.com",
54+
"obtenantbackuppolicies.oceanbase.oceanbase.com",
55+
}
56+
57+
dashboardResources = "oceanbase-dashboard"
58+
59+
localPathProvisionerResources = "local-path-provisioner"
60+
)
61+
2562
// CheckTenantStatus checks running status of obtenant
2663
func CheckTenantStatus(tenant *v1alpha1.OBTenant) error {
2764
if tenant.Status.Status != tenantstatus.Running {
@@ -53,3 +90,38 @@ func CheckTenantRole(tenant *v1alpha1.OBTenant, role apitypes.TenantRole) error
5390
}
5491
return nil
5592
}
93+
94+
// CheckIfComponentExists checks if component exists in the environment
95+
func CheckIfComponentExists(component string) bool {
96+
switch component {
97+
case "cert-manager":
98+
return checkIfResourceExists(certManagerCheckCmd, certManagerResources...)
99+
case "ob-operator":
100+
return checkIfResourceExists(operatorCheckCmd, operatorResources...)
101+
case "ob-dashboard":
102+
return checkIfResourceExists(dashboardCheckCmd, dashboardResources)
103+
case "local-path-provisioner":
104+
return checkIfResourceExists(localPathProvisionerCheckCmd, localPathProvisionerResources)
105+
default:
106+
return false
107+
}
108+
}
109+
110+
// checkIfResourceExists checks if the resource exists in the environment
111+
func checkIfResourceExists(checkCmd string, resourceList ...string) bool {
112+
cmd := exec.Command("sh", "-c", checkCmd)
113+
var out bytes.Buffer
114+
cmd.Stdout = &out
115+
err := cmd.Run()
116+
117+
if err != nil {
118+
return false
119+
}
120+
121+
for _, resource := range resourceList {
122+
if !bytes.Contains(out.Bytes(), []byte(resource)) {
123+
return false
124+
}
125+
}
126+
return true
127+
}

0 commit comments

Comments
 (0)