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

Add items to parse functionallity to the mappings #44

Merged
merged 5 commits into from
May 6, 2024

Conversation

itamar-smirra-port
Copy link
Contributor

@itamar-smirra-port itamar-smirra-port commented Apr 11, 2024

Description

What - Add items to parse functionallity to the mappings
Why - To ingest inner entities from base resource

Type of change

Please leave one option from the following and delete the rest:

  • New feature (non-breaking change which adds functionality)

The current mapping:

port:
      itemsToParse: .spec.template.spec.containers
      entity:
        mappings:
          - identifier: .item.name + "-Container"
            title: .item.name
            blueprint: '"container"'
            icon: '"Deployment"'
            properties:
              image: .item.image
            relations:
              deployment: .metadata.name + "-Deployment-" + .metadata.namespace

Will ingest the containers inside a Kubernetes deployment.
Example deployment yaml with two containers:

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "2"
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"app":"nginx"},"name":"nginx-deployment","namespace":"default"},"spec":{"replicas":3,"selector":{"matchLabels":{"app":"nginx"}},"template":{"metadata":{"labels":{"app":"nginx"}},"spec":{"containers":[{"image":"nginx:1.14.2","name":"nginx","ports":[{"containerPort":80}]}]}}}}
  creationTimestamp: "2024-04-11T14:28:11Z"
  generation: 2
  labels:
    app: nginx
    itamar: hamelech
  name: nginx-deployment
  namespace: default
  resourceVersion: "17669"
  uid: 177fb3eb-fcab-4ae0-b8e9-87fc927d9f03
spec:
  progressDeadlineSeconds: 600
  replicas: 3
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: nginx
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:1.14.2
        imagePullPolicy: IfNotPresent
        name: nginx
        ports:
        - containerPort: 80
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      - image: itamar:1.14.2
        imagePullPolicy: IfNotPresent
        name: itamar
        ports:
        - containerPort: 80
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 3
  conditions:
  - lastTransitionTime: "2024-04-11T14:28:20Z"
    lastUpdateTime: "2024-04-11T14:28:20Z"
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  - lastTransitionTime: "2024-04-11T15:51:32Z"
    lastUpdateTime: "2024-04-11T15:51:32Z"
    message: ReplicaSet "nginx-deployment-5b8b448b96" has timed out progressing.
    reason: ProgressDeadlineExceeded
    status: "False"
    type: Progressing
  observedGeneration: 2
  readyReplicas: 3
  replicas: 4
  unavailableReplicas: 1
  updatedReplicas: 1

Will create in Port:
Screenshot 2024-04-14 at 14 54 48

Copy link
Contributor

@yairsimantov20 yairsimantov20 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comments

@@ -193,7 +193,8 @@ type EntityMappings struct {
}

type Port struct {
Entity EntityMappings `json:"entity"`
Entity EntityMappings `json:"entity"`
ItemsToParse string `json:"ItemsToParse"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ItemsToParse string `json:"ItemsToParse"`
ItemsToParse string `json:"itemsToParse"`

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we need to add support for yaml in here

Copy link
Contributor Author

@itamar-smirra-port itamar-smirra-port Apr 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general or just for the itemsToParse?
If in general it maybe be better to create separate task for it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its not that big of a deal
just add yaml:"" and the name should be same as json

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, done.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we talked about it
yaml

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

pkg/k8s/controller.go Outdated Show resolved Hide resolved
if err != nil {
return nil, fmt.Errorf("invalid selector query '%s': %v", selector.Query, err)
entities := make([]port.Entity, 0, len(mappings))
if itemsToParse != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this if is redundant
instead always runs the loop and set the single value to an array in case there is no items to parse

Copy link
Contributor Author

@itamar-smirra-port itamar-smirra-port Apr 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need the if so I can check wether the items array parsing is needed, but I had fix it to use only one loop as you suggest with the nice idea of insert the single object into array.
Let me know if that what you meant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently I had a problem with this solution, that because the items were overriding each other so only the last item was mapped to entity in the later for loop.
I revert it back to map the item to an entity in each item so it would not happened.
Another solution will be to clone each item to the edited object before insert it to the objectsToMap array but there is no build in clone method for Go so I figured it is a bit overkill.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we talked about this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

pkg/port/models.go Outdated Show resolved Hide resolved
Copy link
Contributor

@yairsimantov20 yairsimantov20 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🌊

@itamar-smirra-port itamar-smirra-port merged commit 62df5c3 into main May 6, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants