Skip to content

Commit

Permalink
Merge pull request #21 from gepaplexx/bug/20
Browse files Browse the repository at this point in the history
fix(#20) RBAC collector removes labels from labels configMap
  • Loading branch information
fhochleitner authored Nov 23, 2023
2 parents 46ef662 + 49682fe commit 894726a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
ignore-unfixed: true
format: 'sarif'
output: 'trivy-results.sarif'
severity: 'CRITICAL,HIGH,MEDIUM,LOW'
severity: 'CRITICAL,HIGH'
# exit-code: '1'

- name: Upload Trivy scan results to GitHub Security tab
Expand Down Expand Up @@ -150,7 +150,7 @@ jobs:
input: 'multena-rbac-collector-oci'
format: 'sarif'
output: 'trivy-results.sarif'
severity: 'CRITICAL,HIGH,MEDIUM,LOW'
severity: 'CRITICAL,HIGH'

- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v2
Expand Down
29 changes: 18 additions & 11 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package util

import (
"context"
"fmt"
"gopkg.in/yaml.v3"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"strings"

yaml "gopkg.in/yaml.v3"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
Expand All @@ -15,7 +18,19 @@ func WriteConfigmap(clientset *kubernetes.Clientset, permission map[string]map[s
if err != nil {
return err
}
data := strings.Replace(string(permissions), "\n", "\\n", -1)
patch := []byte(fmt.Sprintf(`{"data":{"labels.yaml": "%s"}}`, data))

_, err = clientset.CoreV1().ConfigMaps(c.CMNamespace).Patch(context.Background(), c.CMName, types.MergePatchType, patch, metav1.PatchOptions{})
if err != nil {
if errors.IsNotFound(err) {
return createConfigMap(clientset, c, permissions)
}
}
return nil
}

func createConfigMap(clientset *kubernetes.Clientset, c Config, permissions []byte) error {
cm := v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: c.CMName,
Expand All @@ -27,16 +42,8 @@ func WriteConfigmap(clientset *kubernetes.Clientset, permission map[string]map[s
},
BinaryData: nil,
}

_, err = clientset.CoreV1().ConfigMaps(c.CMNamespace).Update(context.Background(), &cm, metav1.UpdateOptions{})
_, err := clientset.CoreV1().ConfigMaps(c.CMNamespace).Create(context.Background(), &cm, metav1.CreateOptions{})
if err != nil {
if errors.IsNotFound(err) {
_, err = clientset.CoreV1().ConfigMaps(c.CMNamespace).Create(context.Background(), &cm, metav1.CreateOptions{})
if err != nil {
return err
}
return nil
}
return err
}
return nil
Expand Down

0 comments on commit 894726a

Please sign in to comment.