Skip to content

Commit

Permalink
fix: only remove first occurence of kind (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aatman authored Sep 8, 2021
1 parent d4c9696 commit 1851938
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
20 changes: 20 additions & 0 deletions pkg/kube2cdk8s/.snapshots/kube2cdk8s-TestKube2CDK8SRole
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
new k8s.KubeRoleBinding(this, "argocd-application-controller", {
metadata: {
labels: {
"app.kubernetes.io/component": "application-controller",
"app.kubernetes.io/name": "argocd-application-controller",
"app.kubernetes.io/part-of": "argocd",
},
name: "argocd-application-controller",
},
roleRef: {
apiGroup: "rbac.authorization.k8s.io",
kind: "Role",
name: "argocd-application-controller",
},
subjects: [{
kind: "ServiceAccount",
name: "argocd-application-controller",
}],
});

13 changes: 11 additions & 2 deletions pkg/kube2cdk8s/kube2cdk8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,18 @@ func Kube2CDK8S(filePath string) (string, error) {
res := re.ReplaceAllString(output, name)

re2 := regexp.MustCompile("(?m)[\r\n]+^.*apiVersion.*$")
res2 := re2.ReplaceAllString(res, "")
loc := re2.FindStringIndex(res)
res2 := res
if loc != nil {
res2 = res[:loc[0]] + res[loc[1]:]
}

re3 := regexp.MustCompile("(?m)[\r\n]+^.*kind.*$")
res3 := re3.ReplaceAllString(res2, "")
loc = re3.FindStringIndex(res2)
res3 := res2
if loc != nil {
res3 = res2[:loc[0]] + res2[loc[1]:]
}

defer os.Remove(path)

Expand Down
36 changes: 36 additions & 0 deletions pkg/kube2cdk8s/kube2cdk8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,39 @@ spec:

defer os.Remove(deploymentFile.Name())
}

func TestKube2CDK8SRole(t *testing.T) {

role := `apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
labels:
app.kubernetes.io/component: application-controller
app.kubernetes.io/name: argocd-application-controller
app.kubernetes.io/part-of: argocd
name: argocd-application-controller
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: argocd-application-controller
subjects:
- kind: ServiceAccount
name: argocd-application-controller
`
roleFile, err := util.CreateTempFile([]byte(role))
if err != nil {
log.Println(err.Error())
}

d, err := Kube2CDK8S(roleFile.Name())
if err != nil {
log.Println(err.Error())
}

err = cupaloy.Snapshot(d)
if err != nil {
t.Error(err.Error())
}

defer os.Remove(roleFile.Name())
}

0 comments on commit 1851938

Please sign in to comment.