Skip to content

Commit 020e82a

Browse files
authored
feat: support env variables in file path (#9)
Signed-off-by: Mario Constanti <[email protected]>
1 parent f378693 commit 020e82a

File tree

8 files changed

+93
-50
lines changed

8 files changed

+93
-50
lines changed

Makefile

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -59,39 +59,11 @@ test: fmt vet ## Run tests.
5959

6060
.PHONY: build
6161
build: fmt vet ## Build manager binary.
62-
go build -o bin/manager cmd/main.go
62+
go build -o bin/kubectl-dpm cmd/kubectl-dpm.go
6363

6464
.PHONY: run
6565
run: fmt vet ## Run a controller from your host.
66-
go run ./cmd/main.go
67-
68-
# If you wish built the manager image targeting other platforms you can use the --platform flag.
69-
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
70-
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
71-
.PHONY: docker-build
72-
docker-build: test ## Build docker image with the manager.
73-
$(CONTAINER_TOOL) build -t ${IMG} .
74-
75-
.PHONY: docker-push
76-
docker-push: ## Push docker image with the manager.
77-
$(CONTAINER_TOOL) push ${IMG}
78-
79-
# PLATFORMS defines the target platforms for the manager image be build to provide support to multiple
80-
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
81-
# - able to use docker buildx . More info: https://docs.docker.com/build/buildx/
82-
# - have enable BuildKit, More info: https://docs.docker.com/develop/develop-images/build_enhancements/
83-
# - be able to push the image for your registry (i.e. if you do not inform a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
84-
# To properly provided solutions that supports more than one platform you should use this option.
85-
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
86-
.PHONY: docker-buildx
87-
docker-buildx: test ## Build and push docker image for the manager for cross-platform support
88-
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
89-
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
90-
- $(CONTAINER_TOOL) buildx create --name project-v3-builder
91-
$(CONTAINER_TOOL) buildx use project-v3-builder
92-
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
93-
- $(CONTAINER_TOOL) buildx rm project-v3-builder
94-
rm Dockerfile.cross
66+
go run ./cmd/kubectl-dpm.go
9567

9668
##@ SBOM
9769

demo/debug-profiles/debug-profiles.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
kubectlPath: ${HOME}/bin/kubectl
12
profiles:
23
- name: prometheus
34
profile: /home/comario/go/src/github.com/bavarianbidi/kubectl-dpm/demo/debug-profiles/prom-profile-config.json
@@ -6,7 +7,7 @@ profiles:
67
matchLabels:
78
app.kubernetes.io/name: prometheus
89
- name: webapp
9-
profile: /home/comario/go/src/github.com/bavarianbidi/kubectl-dpm/demo/debug-profiles/app-profile-config.json
10+
profile: $HOME/go/src/github.com/bavarianbidi/kubectl-dpm/demo/debug-profiles/app-profile-config.json
1011
image: nicolaka/netshoot:v0.13
1112
namespace: default
1213
matchLabels:

pkg/command/debug.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,10 @@ func (o *CustomDebugOptions) run(streams genericiooptions.IOStreams, args []stri
124124
func(c profile.Profile) bool { return c.ProfileName == flagProfileName },
125125
)
126126

127-
fmt.Printf("Profile: %+v\n", debugProfile)
128-
129127
debugProfile = profile.Config.Profiles[idx]
130128

129+
fmt.Printf("Profile: %+v\n", debugProfile)
130+
131131
// this is not needed atm as we want support kubectl versions < 1.30
132132
//
133133
// to just wrap the kubectl-debug command, we have to change a few things in here
@@ -144,7 +144,7 @@ func (o *CustomDebugOptions) run(streams genericiooptions.IOStreams, args []stri
144144
}
145145

146146
func (o *CustomDebugOptions) prepareEphemeralContainer(streams genericiooptions.IOStreams, args []string) error {
147-
o.DebugOptions.CustomProfileFile = debugProfile.CustomProfileFile
147+
o.DebugOptions.CustomProfileFile = os.ExpandEnv(debugProfile.CustomProfileFile)
148148
o.DebugOptions.Profile = kubectldebug.ProfileLegacy
149149

150150
// use image from profile

pkg/command/validate.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
package command
44

55
import (
6-
"log"
6+
"fmt"
77

88
"github.com/spf13/cobra"
99

@@ -25,9 +25,7 @@ func ValidateDebugProfileFile() *cobra.Command {
2525
return err
2626
}
2727

28-
log.Printf("all profiles are valid\n")
29-
log.Printf("kubectl path: %s\n", profile.Config.KubectlPath)
30-
log.Printf("profiles: %v\n", profile.Config.Profiles)
28+
fmt.Printf("all profiles are valid\n")
3129

3230
return nil
3331
},
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"volumeMounts": [
3+
{
4+
"mountPath": "/app/config",
5+
"name": "app-config",
6+
"readOnly": true
7+
}
8+
],
9+
"securityContext": {
10+
"capabilities": {
11+
"add": [
12+
"CAP_NET_ADMIN"
13+
]
14+
}
15+
},
16+
"env": [
17+
{
18+
"name": "APP_ENV",
19+
"value": "test"
20+
}
21+
]
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"volumeMounts": [
3+
{
4+
"mountPath": "/app/config",
5+
"name": "app-config",
6+
"readOnly": true
7+
}
8+
],
9+
"securityContext": {
10+
"capabilities": {
11+
"add": [
12+
"CAP_NET_ADMIN"
13+
]
14+
}
15+
},
16+
"env": [
17+
{
18+
"name": "APP_ENV",
19+
"value": "prod"
20+
},
21+
{
22+
"name": "APP_DB",
23+
"value": "sql://prod"
24+
}
25+
]
26+
}

pkg/profile/validate.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package profile
44

55
import (
66
"cmp"
7+
"encoding/json"
78
"fmt"
89
"log"
910
"os"
@@ -35,7 +36,7 @@ func ValidateKubectlPath() error {
3536
Config.KubectlPath = os.Getenv("_")
3637
} else {
3738
// check if configured kubectl path is valid
38-
info, err := os.Stat(Config.KubectlPath)
39+
info, err := os.Stat(os.ExpandEnv(Config.KubectlPath))
3940
if os.IsNotExist(err) {
4041
return fmt.Errorf("kubectl %s does not exist", Config.KubectlPath)
4142
}
@@ -88,18 +89,41 @@ func ValidateAllProfiles() error {
8889
// This function is not implemented yet
8990
// future ideas: check if the profile file exists and
9091
// it's a valid pod.spec
91-
func ValidateProfile(_ string) error {
92+
func ValidateProfile(profileName string) error {
93+
idx := slices.IndexFunc(Config.Profiles,
94+
func(c Profile) bool { return c.ProfileName == profileName },
95+
)
96+
97+
if err := validatePodSpec(Config.Profiles[idx].CustomProfileFile); err != nil {
98+
return err
99+
}
100+
92101
return nil
93102
}
94103

95-
func ValidateAndCompleteProfile(profileName string) error {
96-
if err := ValidateProfile(profileName); err != nil {
104+
func validatePodSpec(podSpec string) error {
105+
podSpecByte, err := os.ReadFile(os.ExpandEnv(podSpec))
106+
if err != nil {
97107
return err
98108
}
99109

110+
pod := corev1.PodSpec{}
111+
112+
if err := json.Unmarshal(podSpecByte, &pod); err != nil {
113+
return err
114+
}
115+
116+
return nil
117+
}
118+
119+
func ValidateAndCompleteProfile(profileName string) error {
100120
if err := CompleteProfile(profileName); err != nil {
101121
return err
102122
}
123+
124+
if err := ValidateProfile(profileName); err != nil {
125+
return err
126+
}
103127
return nil
104128
}
105129

pkg/profile/validate_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,34 +67,34 @@ func TestValidateAllProfiles(t *testing.T) {
6767
Profiles: []Profile{
6868
{
6969
ProfileName: "profile1",
70-
CustomProfileFile: "profile1.yaml",
70+
CustomProfileFile: "test_data/profile1.json",
7171
Image: "busybox",
7272
Namespace: "default",
7373
ImagePullPolicy: "Always",
7474
},
7575
{
7676
ProfileName: "profile2",
77-
CustomProfileFile: "profile2.yaml",
77+
CustomProfileFile: "test_data/profile2.json",
7878
Image: "busybox",
7979
},
8080
{
8181
ProfileName: "profile2",
82-
CustomProfileFile: "profile2.yaml",
82+
CustomProfileFile: "test_data/profile2.json",
8383
},
8484
},
8585
},
8686
wantedConfig: CustomDebugProfile{
8787
Profiles: []Profile{
8888
{
8989
ProfileName: "profile1",
90-
CustomProfileFile: "profile1.yaml",
90+
CustomProfileFile: "test_data/profile1.json",
9191
Image: "busybox",
9292
Namespace: "default",
9393
ImagePullPolicy: "Always",
9494
},
9595
{
9696
ProfileName: "profile2",
97-
CustomProfileFile: "profile2.yaml",
97+
CustomProfileFile: "test_data/profile2.json",
9898
Image: "busybox",
9999
},
100100
},
@@ -106,7 +106,7 @@ func TestValidateAllProfiles(t *testing.T) {
106106
Profiles: []Profile{
107107
{
108108
ProfileName: "profile1",
109-
CustomProfileFile: "profile1.yaml",
109+
CustomProfileFile: "test_data/profile1.json",
110110
Image: "busybox",
111111
Namespace: "default",
112112
ImagePullPolicy: "Always",
@@ -124,13 +124,13 @@ func TestValidateAllProfiles(t *testing.T) {
124124
Profiles: []Profile{
125125
{
126126
ProfileName: "profile1",
127-
CustomProfileFile: "profile1.yaml",
127+
CustomProfileFile: "test_data/profile1.json",
128128
Image: "busybox",
129129
Namespace: "default",
130130
ImagePullPolicy: "Always",
131131
},
132132
{
133-
CustomProfileFile: "profile2.yaml",
133+
CustomProfileFile: "test_data/profile2.json",
134134
},
135135
},
136136
},

0 commit comments

Comments
 (0)