Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PB-4580: Change TransformResources resource collector code to accept the array index in the path #1533

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sgajawada-px
Copy link
Contributor

@sgajawada-px sgajawada-px commented Oct 13, 2023

  • Change TransformResources resource collector code to accept the array index in the path

What type of PR is this?

Uncomment only one and also add the corresponding label in the PR:
bug
feature
improvement
cleanup
api-change
design
documentation
failing-test
unit-test
integration-test

What this PR does / why we need it:
Added a wrapper code to unstructured module to support the array index in the transform spec path

Does this PR change a user-facing CRD or CLI?:
no

Is a release note needed?:
YES

Issue: Array Index in the transform spec is not supported before
User Impact: Unable to alter the resources under a slice using the resource transform feature
Resolution:
Now if the path is supported with array index like below
spec.containers[0].image

Does this change need to be cherry-picked to a release branch?:
YES

Below is the spec used for testing

apiVersion: stork.libopenstorage.org/v1alpha1
kind: ResourceTransformation
metadata:
  name: mysql-pod-transform
  namespace: default
  specs:
  transformSpecs:
    - paths:
        - operation: modify
          path: spec.replicas
          type: int
          value: '3'
        - operation: delete
          path: spec.replicas
          type: int
          value: ''
        - operation: add
          path: spec.replicas
          type: int
          value: '1'
        - operation: modify
          path: spec.template.spec.containers[0].name
          type: string
          value: web2
        - operation: add
          path: spec.template.spec.containers[0].ports[1].name
          type: string
          value: https
        - operation: add
          path: spec.template.spec.containers[0].ports[1].containerPort
          type: int
          value: '443'
        - operation: add
          path: spec.template.spec.containers[0].ports[1].protocol
          type: string
          value: TCP
        - operation: add
          path: spec.template.spec.containers[0].command
          type: slice
          value: echo,1,2,3,4
        - operation: modify
          path: spec.template.spec.containers[0].command
          type: slice
          value: '5'
        - operation: add
          path: spec.template.spec.setHostnameAsFQDN
          type: bool
          value: 'True'
        - operation: add
          path: spec.template.spec.containers[0].securityContext
          type: keypair
          value: procMount:procMount
        - operation: modify
          path: spec.template.spec.containers[0]
          type: keypair
          value: imagePullPolicy:Always
        - operation: add
          path: spec.template.spec.containers[0].ports[2]
          type: keypair
          value: name:web,protocol:TCP
        - operation: add
          path: spec.template.spec.containers[0].ports[2].containerPort
          type: int
          value: '8080'
        - operation: delete
          path: spec.template.spec.containers[0].ports[1]
          type: keypair
          value: ''
        - operation: delete
          path: spec.template.spec.containers[0].command[1]
          type: string
          value: ''
        - operation: add
          path: spec.template.spec.containers[0].command[1]
          type: string
          value: '10'
        - operation: modify
          path: spec.template.spec.containers[0].command[2]
          type: string
          value: '9'
      resource: apps/v1/Deployment
      selectors: null

Unit Test Results:
Screenshot from 2023-10-13 20-20-28
Screenshot from 2023-10-13 20-20-17
Screenshot from 2023-10-13 19-52-38

CR Obejct

apiVersion: stork.libopenstorage.org/v1alpha1
kind: ResourceTransformation
metadata:
  creationTimestamp: '2023-10-20T08:18:08Z'
  finalizers:
    - stork.libopenstorage.org/finalizer-cleanup
  generation: 4
  managedFields:
    - apiVersion: stork.libopenstorage.org/v1alpha1
      fieldsType: FieldsV1
      fieldsV1:
        f:specs:
          .: {}
          f:transformSpecs: {}
      manager: kubectl-replace
      operation: Update
      time: '2023-10-20T08:18:08Z'
    - apiVersion: stork.libopenstorage.org/v1alpha1
      fieldsType: FieldsV1
      fieldsV1:
        f:metadata:
          f:finalizers:
            .: {}
            v:"stork.libopenstorage.org/finalizer-cleanup": {}
        f:status:
          .: {}
          f:resources: {}
          f:status: {}
      manager: stork
      operation: Update
      time: '2023-10-20T08:18:09Z'
  name: mysql-pod-transform
  namespace: default
  resourceVersion: '7681963'
  uid: ce10279a-4557-482e-9ddd-f93ca4abec40
  selfLink: >-
    /apis/stork.libopenstorage.org/v1alpha1/namespaces/default/resourcetransformations/mysql-pod-transform
status:
  resources:
    - group: apps
      kind: Deployment
      name: demo-deployment
      namespace: default
      reason: ''
      specs:
        paths:
          - operation: modify
            path: spec.replicas
            type: int
            value: '3'
          - operation: delete
            path: spec.replicas
            type: int
            value: ''
          - operation: add
            path: spec.replicas
            type: int
            value: '1'
          - operation: modify
            path: spec.template.spec.containers[0].name
            type: string
            value: web2
          - operation: add
            path: spec.template.spec.containers[0].ports[1].name
            type: string
            value: https
          - operation: add
            path: spec.template.spec.containers[0].ports[1].containerPort
            type: int
            value: '443'
          - operation: add
            path: spec.template.spec.containers[0].ports[1].protocol
            type: string
            value: TCP
          - operation: add
            path: spec.template.spec.containers[0].command
            type: slice
            value: echo,1,2,3,4
          - operation: modify
            path: spec.template.spec.containers[0].command
            type: slice
            value: '5'
          - operation: add
            path: spec.template.spec.setHostnameAsFQDN
            type: bool
            value: 'True'
          - operation: add
            path: spec.template.spec.containers[0].securityContext
            type: keypair
            value: procMount:procMount
          - operation: modify
            path: spec.template.spec.containers[0]
            type: keypair
            value: imagePullPolicy:Always
          - operation: add
            path: spec.template.spec.containers[0].ports[2]
            type: keypair
            value: name:web,protocol:TCP
          - operation: add
            path: spec.template.spec.containers[0].ports[2].containerPort
            type: int
            value: '8080'
          - operation: delete
            path: spec.template.spec.containers[0].ports[1]
            type: keypair
            value: ''
          - operation: delete
            path: spec.template.spec.containers[0].command[1]
            type: string
            value: ''
          - operation: add
            path: spec.template.spec.containers[0].command[1]
            type: string
            value: '10'
          - operation: modify
            path: spec.template.spec.containers[0].command[2]
            type: string
            value: '9'
        resource: apps/v1/Deployment
        selectors: null
      status: Ready
      version: v1
  status: Ready
specs:
  transformSpecs:
    - paths:
        - operation: modify
          path: spec.replicas
          type: int
          value: '3'
        - operation: delete
          path: spec.replicas
          type: int
          value: ''
        - operation: add
          path: spec.replicas
          type: int
          value: '1'
        - operation: modify
          path: spec.template.spec.containers[0].name
          type: string
          value: web2
        - operation: add
          path: spec.template.spec.containers[0].ports[1].name
          type: string
          value: https
        - operation: add
          path: spec.template.spec.containers[0].ports[1].containerPort
          type: int
          value: '443'
        - operation: add
          path: spec.template.spec.containers[0].ports[1].protocol
          type: string
          value: TCP
        - operation: add
          path: spec.template.spec.containers[0].command
          type: slice
          value: echo,1,2,3,4
        - operation: modify
          path: spec.template.spec.containers[0].command
          type: slice
          value: '5'
        - operation: add
          path: spec.template.spec.setHostnameAsFQDN
          type: bool
          value: 'True'
        - operation: add
          path: spec.template.spec.containers[0].securityContext
          type: keypair
          value: procMount:procMount
        - operation: modify
          path: spec.template.spec.containers[0]
          type: keypair
          value: imagePullPolicy:Always
        - operation: add
          path: spec.template.spec.containers[0].ports[2]
          type: keypair
          value: name:web,protocol:TCP
        - operation: add
          path: spec.template.spec.containers[0].ports[2].containerPort
          type: int
          value: '8080'
        - operation: delete
          path: spec.template.spec.containers[0].ports[1]
          type: keypair
          value: ''
        - operation: delete
          path: spec.template.spec.containers[0].command[1]
          type: string
          value: ''
        - operation: add
          path: spec.template.spec.containers[0].command[1]
          type: string
          value: '10'
        - operation: modify
          path: spec.template.spec.containers[0].command[2]
          type: string
          value: '9'
      resource: apps/v1/Deployment
      selectors: null

Transformed unstructured object for reference

&{map[
apiVersion:apps/v1 
kind:Deployment 
metadata:map[
annotations:map[deployment.kubernetes.io/revision:1 stork.libopenstorage.org/resourcetransformation-name:true] name:demo-deployment namespace:default] 
spec:map[
progressDeadlineSeconds:600 
replicas:1 
revisionHistoryLimit:10 
selector:map[matchLabels:map[app:demo]] 
strategy:map[rollingUpdate:map[maxSurge:25% maxUnavailable:25%] type:RollingUpdate] 
template:map[
metadata:map[creationTimestamp:<nil> labels:map[app:demo]] 
spec:map[
containers:[map[
command:[echo 10 9 4 5] 
image:nginx:1.12 
imagePullPolicy:Always 
name:web2 
ports:[
map[containerPort:80 name:http protocol:TCP] 
map[containerPort:8080 name:web protocol:TCP]] 
resources:map[] 
securityContext:map[procMount:procMount] 
terminationMessagePath:/dev/termination-log terminationMessagePolicy:File]] 
dnsPolicy:ClusterFirst 
restartPolicy:Always schedulerName:default-scheduler securityContext:map[] setHostnameAsFQDN:true terminationGracePeriodSeconds:30]]] 

status:map[availableReplicas:2 conditions:[map[lastTransitionTime:2023-10-09T10:41:07Z lastUpdateTime:2023-10-09T10:41:20Z message:ReplicaSet \"demo-deployment-5f8f46f749\" has successfully progressed. reason:NewReplicaSetAvailable status:True type:Progressing] map[lastTransitionTime:2023-10-18T23:44:28Z lastUpdateTime:2023-10-18T23:44:28Z message:Deployment has minimum availability. reason:MinimumReplicasAvailable status:True type:Available]] observedGeneration:1 readyReplicas:2 replicas:2 updatedReplicas:2]]}"

@sgajawada-px sgajawada-px added bug release-note Information about this change needs to be added to the release note labels Oct 13, 2023
@cnbu-jenkins
Copy link
Collaborator

Can one of the admins verify this patch?

@codecov
Copy link

codecov bot commented Oct 18, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (56aa1f2) 65.93% compared to head (5b7a460) 65.04%.
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1533      +/-   ##
==========================================
- Coverage   65.93%   65.04%   -0.90%     
==========================================
  Files          43       43              
  Lines        5349     5621     +272     
==========================================
+ Hits         3527     3656     +129     
- Misses       1490     1625     +135     
- Partials      332      340       +8     

see 3 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

   - Change TransformResources resource collector code to accept the array index in the path
@sgajawada-px sgajawada-px changed the title PB-4580: PB-4580: Change TransformResources resource collector code to accept the array index in the path Oct 20, 2023
@sgajawada-px sgajawada-px removed the bug label Oct 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note Information about this change needs to be added to the release note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants