From c6b51649b20bfafb8e5dbf90666245b254a7efd7 Mon Sep 17 00:00:00 2001 From: joyceliu Date: Fri, 15 Nov 2024 16:11:35 +0800 Subject: [PATCH] fix: test kk push image to harbor. --- builtin/playbooks/artifact_images.yaml | 9 ++++-- .../install/image-registry/defaults/main.yaml | 2 ++ .../image-registry/tasks/load_images.yaml | 28 ++++++++++--------- .../app/options/controller_manager.go | 2 +- cmd/kk/app/options/option.go | 2 +- cmd/kk/app/options/pipeline.go | 2 +- pkg/executor/pipeline_executor.go | 2 +- 7 files changed, 27 insertions(+), 20 deletions(-) diff --git a/builtin/playbooks/artifact_images.yaml b/builtin/playbooks/artifact_images.yaml index a4892e3f9..438f243bf 100644 --- a/builtin/playbooks/artifact_images.yaml +++ b/builtin/playbooks/artifact_images.yaml @@ -2,6 +2,9 @@ - localhost tags: ["always"] roles: - - init/init-artifact - - init/init-cert - - install/image-registry + - role: init/init-artifact + tags: ["always"] + - role: init/init-cert + tags: ["always"] + - role: install/image-registry + tags: ["always"] diff --git a/builtin/roles/install/image-registry/defaults/main.yaml b/builtin/roles/install/image-registry/defaults/main.yaml index a40c2af1c..d95b121a3 100644 --- a/builtin/roles/install/image-registry/defaults/main.yaml +++ b/builtin/roles/install/image-registry/defaults/main.yaml @@ -1,6 +1,8 @@ image_registry: # ha_vip: 192.168.122.59 namespace_override: "" + # which store images data which will push to registry. + images_dir: /tmp/kubekey/images/ auth: registry: | {{- if and .image_registry.ha_vip (ne .image_registry.ha_vip "") }} diff --git a/builtin/roles/install/image-registry/tasks/load_images.yaml b/builtin/roles/install/image-registry/tasks/load_images.yaml index dee13e147..f429e6a4b 100644 --- a/builtin/roles/install/image-registry/tasks/load_images.yaml +++ b/builtin/roles/install/image-registry/tasks/load_images.yaml @@ -4,13 +4,14 @@ copy: src: | {{ .work_dir }}/kubekey/images/ - dest: /tmp/kubekey/images/ + dest: | + {{ .image_registry.images_dir }} - name: Create harbor project for each image tags: ["only_image"] command: | {{- if .image_registry.namespace_override | eq "" }} - for dir in /tmp/kubekey/images/*; do + for dir in {{ .image_registry.images_dir }}*; do if [ ! -d "$dir" ]; then # only deal with directories continue @@ -18,24 +19,24 @@ project=${dir##*/} - if [ "$project" == "blobs" ]; then + if [[ "$project" == "blobs" ]]; then # skip blobs dir continue fi - - # if project is not exist, create if - http_code=$(curl -Iks -u "{{ .image_registry.auth.username }}:{{ .image_registry.auth.password }}" 'https://localhost/api/v2.0/projects?project_name=${project}' | grep HTTP | awk '{print $2}') - if [ $http_code == 404 ]; then + + # if project is not exist, create it + resp=$(curl -u "{{ .image_registry.auth.username }}:{{ .image_registry.auth.password }}" -k -X GET "https://{{ .image_registry.auth.registry }}/api/v2.0/projects/${project}") + if echo "$resp" | grep -q '"code":"NOT_FOUND"'; then # create project - curl -u "{{ .image_registry.auth.username }}:{{ .image_registry.auth.password }}" -k -X POST -H "Content-Type: application/json" "https://localhost/api/v2.0/projects" -d "{ \"project_name\": \"${project}\", \"public\": true}" + curl -u "{{ .image_registry.auth.username }}:{{ .image_registry.auth.password }}" -k -X POST -H "Content-Type: application/json" "https://{{ .image_registry.auth.registry }}/api/v2.0/projects" -d "{ \"project_name\": \"${project}\", \"public\": true}" fi done {{- else }} - # if project is not exist, create if - http_code=$(curl -Iks -u "{{ .image_registry.auth.username }}:{{ .image_registry.auth.password }}" 'https://localhost/api/v2.0/projects?project_name={{ .image_registry.namespace_override }}' | grep HTTP | awk '{print $2}') - if [ $http_code == 404 ]; then + # if project is not exist, create it + resp=$(curl -u "{{ .image_registry.auth.username }}:{{ .image_registry.auth.password }}" -k -X GET "https://{{ .image_registry.auth.registry }}/api/v2.0/projects/{{ .image_registry.namespace_override }}") + if echo "$resp" | grep -q '"code":"NOT_FOUND"'; then # create project - curl -u "{{ .image_registry.auth.username }}:{{ .image_registry.auth.password }}" -k -X POST -H "Content-Type: application/json" "https://localhost/api/v2.0/projects" -d "{ \"project_name\": \"{{ .image_registry.namespace_override }}\", \"public\": true}" + curl -u "{{ .image_registry.auth.username }}:{{ .image_registry.auth.password }}" -k -X POST -H "Content-Type: application/json" "https://{{ .image_registry.auth.registry }}/api/v2.0/projects" -d "{ \"project_name\": \"{{ .image_registry.namespace_override }}\", \"public\": true}" fi {{- end }} when: .image_registry.type | eq "harbor" @@ -44,7 +45,8 @@ tags: ["only_image"] image: push: - images_dir: /tmp/kubekey/images/ + images_dir: | + {{ .image_registry.images_dir }} registry: | {{ .image_registry.auth.registry }} namespace_override: | diff --git a/cmd/controller-manager/app/options/controller_manager.go b/cmd/controller-manager/app/options/controller_manager.go index bcd680d82..fc9c2c1cb 100644 --- a/cmd/controller-manager/app/options/controller_manager.go +++ b/cmd/controller-manager/app/options/controller_manager.go @@ -43,7 +43,7 @@ func NewControllerManagerServerOptions() *ControllerManagerServerOptions { func (o *ControllerManagerServerOptions) Flags() cliflag.NamedFlagSets { fss := cliflag.NamedFlagSets{} gfs := fss.FlagSet("generic") - gfs.StringVar(&o.WorkDir, "work-dir", o.WorkDir, "the base Dir for kubekey. Default current dir. ") + gfs.StringVar(&o.WorkDir, "workdir", o.WorkDir, "the base Dir for kubekey. Default current dir. ") gfs.BoolVar(&o.Debug, "debug", o.Debug, "Debug mode, after a successful execution of Pipeline, "+"will retain runtime data, which includes task execution status and parameters.") cfs := fss.FlagSet("controller-manager") cfs.IntVar(&o.MaxConcurrentReconciles, "max-concurrent-reconciles", o.MaxConcurrentReconciles, "The number of maximum concurrent reconciles for controller.") diff --git a/cmd/kk/app/options/option.go b/cmd/kk/app/options/option.go index 4cae24f5a..a3f0e9adf 100644 --- a/cmd/kk/app/options/option.go +++ b/cmd/kk/app/options/option.go @@ -84,7 +84,7 @@ func newCommonOptions() commonOptions { func (o *commonOptions) flags() cliflag.NamedFlagSets { fss := cliflag.NamedFlagSets{} gfs := fss.FlagSet("generic") - gfs.StringVar(&o.WorkDir, "work-dir", o.WorkDir, "the base Dir for kubekey. Default current dir. ") + gfs.StringVar(&o.WorkDir, "workdir", o.WorkDir, "the base Dir for kubekey. Default current dir. ") gfs.StringVarP(&o.Artifact, "artifact", "a", "", "Path to a KubeKey artifact") gfs.StringVarP(&o.ConfigFile, "config", "c", o.ConfigFile, "the config file path. support *.yaml ") gfs.StringArrayVar(&o.Set, "set", o.Set, "set value in config. format --set key=val or --set k1=v1,k2=v2") diff --git a/cmd/kk/app/options/pipeline.go b/cmd/kk/app/options/pipeline.go index 0624fb8aa..e0bb610a0 100644 --- a/cmd/kk/app/options/pipeline.go +++ b/cmd/kk/app/options/pipeline.go @@ -26,7 +26,7 @@ func (o *PipelineOptions) Flags() cliflag.NamedFlagSets { pfs := fss.FlagSet("pipeline flags") pfs.StringVar(&o.Name, "name", o.Name, "name of pipeline") pfs.StringVarP(&o.Namespace, "namespace", "n", o.Namespace, "namespace of pipeline") - pfs.StringVar(&o.WorkDir, "work-dir", o.WorkDir, "the base Dir for kubekey. Default current dir. ") + pfs.StringVar(&o.WorkDir, "workdir", o.WorkDir, "the base Dir for kubekey. Default current dir. ") return fss } diff --git a/pkg/executor/pipeline_executor.go b/pkg/executor/pipeline_executor.go index c66878e59..7af4e994a 100644 --- a/pkg/executor/pipeline_executor.go +++ b/pkg/executor/pipeline_executor.go @@ -132,7 +132,7 @@ func (e pipelineExecutor) execBatchHosts(ctx context.Context, play kkprojectv1.P } // generate task from role for _, role := range play.Roles { - if !role.Taggable.IsEnabled(e.pipeline.Spec.Tags, e.pipeline.Spec.SkipTags) { + if !kkprojectv1.JoinTag(role.Taggable, play.Taggable).IsEnabled(e.pipeline.Spec.Tags, e.pipeline.Spec.SkipTags) { // if not match the tags. skip continue }