From 3b022cfd008b9cbfb8cb012476fd8171f4e994ae Mon Sep 17 00:00:00 2001 From: FingerLiu Date: Sun, 14 Feb 2021 20:26:02 +0800 Subject: [PATCH] [ADD] image parameter to ke install npd Signed-off-by: FingerLiu --- cmd/add.go | 5 ++++- examples/daemonSet.yaml | 4 ++-- pkg/validator/add.go | 26 +++++++++++++++++++++++--- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/cmd/add.go b/cmd/add.go index 4fa4dca..972bf37 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -22,11 +22,13 @@ import ( "kubeeye/pkg/validator" ) +var npdImage string + var addCmd = &cobra.Command{ Use: "install npd", Short: "install the npd", Run: func(cmd *cobra.Command, args []string) { - err := validator.Add(cmd.Context()) + err := validator.Add(cmd.Context(), npdImage) if err != nil { fmt.Println(err) } @@ -36,4 +38,5 @@ var addCmd = &cobra.Command{ func init() { rootCmd.AddCommand(addCmd) pflag.CommandLine.AddGoFlagSet(flag.CommandLine) + addCmd.Flags().StringVarP(&npdImage, "image", "i", "k8s.gcr.io/node-problem-detector:v0.8.1", "Customize npd image") } diff --git a/examples/daemonSet.yaml b/examples/daemonSet.yaml index fe56cb4..23d4bce 100644 --- a/examples/daemonSet.yaml +++ b/examples/daemonSet.yaml @@ -22,7 +22,7 @@ spec: - --logtostderr - --apiserver-wait-timeout=10s - --config.system-log-monitor=/config/kernel-monitor.json,/config/docker-monitor.json - image: k8s.gcr.io/node-problem-detector:v0.8.1 + image: {{.NpdImage}} resources: limits: cpu: 10m @@ -71,4 +71,4 @@ spec: - key: kernel-monitor.json path: kernel-monitor.json - key: docker-monitor.json - path: docker-monitor.json \ No newline at end of file + path: docker-monitor.json diff --git a/pkg/validator/add.go b/pkg/validator/add.go index 1933fdf..13b2a9e 100644 --- a/pkg/validator/add.go +++ b/pkg/validator/add.go @@ -29,13 +29,20 @@ import ( "k8s.io/apimachinery/pkg/util/yaml" "k8s.io/client-go/kubernetes" "sigs.k8s.io/controller-runtime/pkg/client/config" + "text/template" ) var configBox = (*packr.Box)(nil) -func Add(ctx context.Context) error { +type ImageName struct { + NpdImage string +} + +func Add(ctx context.Context, npdImage string) error { var rawBytes []byte + imageName := ImageName{ NpdImage: npdImage } + // configMap create rawBytes, err := getConfigBox().Find("npd-rule.yaml") if err != nil { @@ -80,11 +87,24 @@ func Add(ctx context.Context) error { return errors.Wrap(err4, "Failed to create clusterRoleBinding") } // daemonSet create - dsBytes, err := getConfigBox().Find("daemonSet.yaml") + var tplWriter bytes.Buffer + dsTmplString, err := getConfigBox().FindString("daemonSet.yaml") if err != nil { return errors.Wrap(err, "Failed to get daemonSet.yaml") } - ds := dsParse(dsBytes) + + dsTemplate, err := template.New("npd").Parse(dsTmplString) + if dsTemplate == nil || err != nil{ + return errors.Wrap(err, "Failed to get daemonSet.yaml template") + } + err = dsTemplate.Execute(&tplWriter, imageName) + if err != nil{ + return errors.Wrap(err, "Failed to render daemonSet.yaml template") + } + + tplWriter.Bytes() + + ds := dsParse(tplWriter.Bytes()) _, err5 := createDaemonSet(ctx, ds) if err5 != nil { return errors.Wrap(err5, "Failed to create daemonSet")