openshift / client-go

Go client for OpenShift
http://www.openshift.org
Apache License 2.0
113 stars 147 forks source link

Not able to Scale DeploymentConfigs using the UpdateScale() function #236

Closed faizanbashir closed 1 year ago

faizanbashir commented 1 year ago

The UpdateScale() function for DeploymentConfigs is not working.

I am using the following code snippet to call the function.

func ScaleDeploymentConfig(client *v1.AppsV1Client, name, namespace string, replica int32) {
    fmt.Printf("Scaling DeploymentConfig: %s in namespace %s to %d\n", name, namespace, replica)
    deploymentConfig := client.DeploymentConfigs(namespace)
    fmt.Printf("%T\n", deploymentConfig)
    scaleObj, err := deploymentConfig.GetScale(context.Background(), name, metav1.GetOptions{})
    if err != nil {
        fmt.Printf("error getting scale object: %v\n", err)
        os.Exit(1)
    }
    sd := *scaleObj
    sd.Spec.Replicas = replica
    fmt.Printf("%+v\n", &sd)
    scaleDeploymentConfigs, err := deploymentConfig.UpdateScale(context.Background(), name, &sd, metav1.UpdateOptions{})
    if err != nil {
        fmt.Printf("error updating scale object: %v\n", err)
        os.Exit(1)
    }
    fmt.Printf("Successfully scaled %+v", scaleDeploymentConfigs)
}

The GetScale() function is working fine, although it throws a warning. Thereafter I am passing the scale object which I am getting from the GetScale() to the update scale function, but it's not working.

Getting the below error

W1214 11:44:07.290708   49036 warnings.go:70] extensions/v1beta1 Scale is deprecated in v1.2+, unavailable in v1.16+
&Scale{ObjectMeta:{example  podkiller  557aa7f3-e5ec-4e93-bbab-ba4f5b598658 1270748657 0 2022-12-11 21:23:30 +0530 IST <nil> <nil> map[] map[] [] [] []},Spec:ScaleSpec{Replicas:2,},Status:ScaleStatus{Replicas:1,Selector:map[string]string{app: busybox,deploymentconfig: example,},TargetSelector:app=busybox,deploymentconfig=example,},}
W1214 11:44:07.653487   49036 warnings.go:70] extensions/v1beta1 Scale is deprecated in v1.2+, unavailable in v1.16+
error updating scale object: the API version in the data (apps.openshift.io/v1) does not match the expected API version (extensions/v1beta1)

Points to consider:

  1. I am using the code in the master branch of the repo. github.com/openshift/api v0.0.0-20221103085154-ea838af1820e
  2. I can scale the deployment configs using the Patch() function defined in the same interface as the UpdateScale(). I have documented it in my blog post.
  3. Running the openshift version command returns the below result:
Client Version: 4.12.0-202208031327
Kustomize Version: v4.5.4
Kubernetes Version: v1.22.8+9e95cb9
faizanbashir commented 1 year ago

I also reached out to Redhat support, but they said there are two ways to scale DeploymentConfigs

  1. Using oc cli command
  2. Using the API
faizanbashir commented 1 year ago

I have solved this issue, although not with the UpdateScale() function. The solution uses the .Patch() function call by passing it a JSONPatch to replace the replicas.

I have documented the solution in my blog post Scaling DeploymentCofigs