Skip to content

Commit

Permalink
feat: support env variables in file path (#9)
Browse files Browse the repository at this point in the history
Signed-off-by: Mario Constanti <github@constanti.de>
  • Loading branch information
bavarianbidi authored Jul 17, 2024
1 parent f378693 commit 020e82a
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 50 deletions.
32 changes: 2 additions & 30 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,39 +59,11 @@ test: fmt vet ## Run tests.

.PHONY: build
build: fmt vet ## Build manager binary.
go build -o bin/manager cmd/main.go
go build -o bin/kubectl-dpm cmd/kubectl-dpm.go

.PHONY: run
run: fmt vet ## Run a controller from your host.
go run ./cmd/main.go

# If you wish built the manager image targeting other platforms you can use the --platform flag.
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
.PHONY: docker-build
docker-build: test ## Build docker image with the manager.
$(CONTAINER_TOOL) build -t ${IMG} .

.PHONY: docker-push
docker-push: ## Push docker image with the manager.
$(CONTAINER_TOOL) push ${IMG}

# PLATFORMS defines the target platforms for the manager image be build to provide support to multiple
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
# - able to use docker buildx . More info: https://docs.docker.com/build/buildx/
# - have enable BuildKit, More info: https://docs.docker.com/develop/develop-images/build_enhancements/
# - 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)
# To properly provided solutions that supports more than one platform you should use this option.
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
.PHONY: docker-buildx
docker-buildx: test ## Build and push docker image for the manager for cross-platform support
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
- $(CONTAINER_TOOL) buildx create --name project-v3-builder
$(CONTAINER_TOOL) buildx use project-v3-builder
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
- $(CONTAINER_TOOL) buildx rm project-v3-builder
rm Dockerfile.cross
go run ./cmd/kubectl-dpm.go

##@ SBOM

Expand Down
3 changes: 2 additions & 1 deletion demo/debug-profiles/debug-profiles.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
kubectlPath: ${HOME}/bin/kubectl
profiles:
- name: prometheus
profile: /home/comario/go/src/github.com/bavarianbidi/kubectl-dpm/demo/debug-profiles/prom-profile-config.json
Expand All @@ -6,7 +7,7 @@ profiles:
matchLabels:
app.kubernetes.io/name: prometheus
- name: webapp
profile: /home/comario/go/src/github.com/bavarianbidi/kubectl-dpm/demo/debug-profiles/app-profile-config.json
profile: $HOME/go/src/github.com/bavarianbidi/kubectl-dpm/demo/debug-profiles/app-profile-config.json
image: nicolaka/netshoot:v0.13
namespace: default
matchLabels:
Expand Down
6 changes: 3 additions & 3 deletions pkg/command/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ func (o *CustomDebugOptions) run(streams genericiooptions.IOStreams, args []stri
func(c profile.Profile) bool { return c.ProfileName == flagProfileName },
)

fmt.Printf("Profile: %+v\n", debugProfile)

debugProfile = profile.Config.Profiles[idx]

fmt.Printf("Profile: %+v\n", debugProfile)

// this is not needed atm as we want support kubectl versions < 1.30
//
// to just wrap the kubectl-debug command, we have to change a few things in here
Expand All @@ -144,7 +144,7 @@ func (o *CustomDebugOptions) run(streams genericiooptions.IOStreams, args []stri
}

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

// use image from profile
Expand Down
6 changes: 2 additions & 4 deletions pkg/command/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package command

import (
"log"
"fmt"

"github.com/spf13/cobra"

Expand All @@ -25,9 +25,7 @@ func ValidateDebugProfileFile() *cobra.Command {
return err
}

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

return nil
},
Expand Down
22 changes: 22 additions & 0 deletions pkg/profile/test_data/profile1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"volumeMounts": [
{
"mountPath": "/app/config",
"name": "app-config",
"readOnly": true
}
],
"securityContext": {
"capabilities": {
"add": [
"CAP_NET_ADMIN"
]
}
},
"env": [
{
"name": "APP_ENV",
"value": "test"
}
]
}
26 changes: 26 additions & 0 deletions pkg/profile/test_data/profile2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"volumeMounts": [
{
"mountPath": "/app/config",
"name": "app-config",
"readOnly": true
}
],
"securityContext": {
"capabilities": {
"add": [
"CAP_NET_ADMIN"
]
}
},
"env": [
{
"name": "APP_ENV",
"value": "prod"
},
{
"name": "APP_DB",
"value": "sql://prod"
}
]
}
32 changes: 28 additions & 4 deletions pkg/profile/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package profile

import (
"cmp"
"encoding/json"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -35,7 +36,7 @@ func ValidateKubectlPath() error {
Config.KubectlPath = os.Getenv("_")
} else {
// check if configured kubectl path is valid
info, err := os.Stat(Config.KubectlPath)
info, err := os.Stat(os.ExpandEnv(Config.KubectlPath))
if os.IsNotExist(err) {
return fmt.Errorf("kubectl %s does not exist", Config.KubectlPath)
}
Expand Down Expand Up @@ -88,18 +89,41 @@ func ValidateAllProfiles() error {
// This function is not implemented yet
// future ideas: check if the profile file exists and
// it's a valid pod.spec
func ValidateProfile(_ string) error {
func ValidateProfile(profileName string) error {
idx := slices.IndexFunc(Config.Profiles,
func(c Profile) bool { return c.ProfileName == profileName },
)

if err := validatePodSpec(Config.Profiles[idx].CustomProfileFile); err != nil {
return err
}

return nil
}

func ValidateAndCompleteProfile(profileName string) error {
if err := ValidateProfile(profileName); err != nil {
func validatePodSpec(podSpec string) error {
podSpecByte, err := os.ReadFile(os.ExpandEnv(podSpec))
if err != nil {
return err
}

pod := corev1.PodSpec{}

if err := json.Unmarshal(podSpecByte, &pod); err != nil {
return err
}

return nil
}

func ValidateAndCompleteProfile(profileName string) error {
if err := CompleteProfile(profileName); err != nil {
return err
}

if err := ValidateProfile(profileName); err != nil {
return err
}
return nil
}

Expand Down
16 changes: 8 additions & 8 deletions pkg/profile/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,34 +67,34 @@ func TestValidateAllProfiles(t *testing.T) {
Profiles: []Profile{
{
ProfileName: "profile1",
CustomProfileFile: "profile1.yaml",
CustomProfileFile: "test_data/profile1.json",
Image: "busybox",
Namespace: "default",
ImagePullPolicy: "Always",
},
{
ProfileName: "profile2",
CustomProfileFile: "profile2.yaml",
CustomProfileFile: "test_data/profile2.json",
Image: "busybox",
},
{
ProfileName: "profile2",
CustomProfileFile: "profile2.yaml",
CustomProfileFile: "test_data/profile2.json",
},
},
},
wantedConfig: CustomDebugProfile{
Profiles: []Profile{
{
ProfileName: "profile1",
CustomProfileFile: "profile1.yaml",
CustomProfileFile: "test_data/profile1.json",
Image: "busybox",
Namespace: "default",
ImagePullPolicy: "Always",
},
{
ProfileName: "profile2",
CustomProfileFile: "profile2.yaml",
CustomProfileFile: "test_data/profile2.json",
Image: "busybox",
},
},
Expand All @@ -106,7 +106,7 @@ func TestValidateAllProfiles(t *testing.T) {
Profiles: []Profile{
{
ProfileName: "profile1",
CustomProfileFile: "profile1.yaml",
CustomProfileFile: "test_data/profile1.json",
Image: "busybox",
Namespace: "default",
ImagePullPolicy: "Always",
Expand All @@ -124,13 +124,13 @@ func TestValidateAllProfiles(t *testing.T) {
Profiles: []Profile{
{
ProfileName: "profile1",
CustomProfileFile: "profile1.yaml",
CustomProfileFile: "test_data/profile1.json",
Image: "busybox",
Namespace: "default",
ImagePullPolicy: "Always",
},
{
CustomProfileFile: "profile2.yaml",
CustomProfileFile: "test_data/profile2.json",
},
},
},
Expand Down

0 comments on commit 020e82a

Please sign in to comment.