Skip to content

Commit

Permalink
support attach metadata in PullImage CRI interface
Browse files Browse the repository at this point in the history
  • Loading branch information
diannaowa committed Feb 24, 2023
1 parent 07c51b9 commit 36c7ef4
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 9 deletions.
4 changes: 4 additions & 0 deletions apis/apps/v1alpha1/imagepulljob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ type ImagePullJobSpec struct {
// CompletionPolicy indicates the completion policy of the job.
// Default is Always CompletionPolicyType.
CompletionPolicy CompletionPolicy `json:"completionPolicy"`

// SandboxConfig support attach metadata in PullImage CRI interface during ImagePulljobs
// +optional
SandboxConfig *SandboxConfig `json:"sandboxConfig,omitempty"`
}

// ImagePullJobPodSelector is a selector over pods
Expand Down
4 changes: 4 additions & 0 deletions apis/apps/v1alpha1/nodeimage_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ type ImageSpec struct {

// Tags is a list of versions of this image
Tags []ImageTagSpec `json:"tags"`

// SandboxConfig support attach metadata in PullImage CRI interface during ImagePulljobs
// +optional
SandboxConfig *SandboxConfig `json:"sandboxConfig,omitempty"`
}

// ReferenceObject comprises a resource name, with a mandatory namespace,
Expand Down
25 changes: 25 additions & 0 deletions apis/apps/v1alpha1/sandboxconfig_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Copyright 2023 The Kruise Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

// SandboxConfig support attach metadata in PullImage CRI interface during ImagePulljobs
type SandboxConfig struct {
// +optional
Labels map[string]string `json:"labels,omitempty"`
// +optional
Annotations map[string]string `json:"annotations,omitempty"`
}
13 changes: 13 additions & 0 deletions config/crd/bases/apps.kruise.io_imagepulljobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,19 @@ spec:
items:
type: string
type: array
sandboxConfig:
description: SandboxConfig support attach metadata in PullImage CRI
interface during ImagePulljobs
properties:
annotations:
additionalProperties:
type: string
type: object
labels:
additionalProperties:
type: string
type: object
type: object
selector:
description: Selector is a query over nodes that should match the
job. nil to match all nodes.
Expand Down
13 changes: 13 additions & 0 deletions config/crd/bases/apps.kruise.io_nodeimages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ spec:
type: string
type: object
type: array
sandboxConfig:
description: SandboxConfig support attach metadata in PullImage
CRI interface during ImagePulljobs
properties:
annotations:
additionalProperties:
type: string
type: object
labels:
additionalProperties:
type: string
type: object
type: object
tags:
description: Tags is a list of versions of this image
items:
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/imagepulljob/imagepulljob_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ func (r *ReconcileImagePullJob) syncNodeImages(job *appsv1alpha1.ImagePullJob, n
ownerRef := getOwnerRef(job)
secrets := getSecrets(job)
pullPolicy := getImagePullPolicy(job)

now := metav1.NewTime(r.clock.Now())
imageName, imageTag, _ := daemonutil.NormalizeImageRefToNameTag(job.Spec.Image)
for i := 0; i < parallelism; i++ {
Expand All @@ -267,6 +266,7 @@ func (r *ReconcileImagePullJob) syncNodeImages(job *appsv1alpha1.ImagePullJob, n
nodeImage.Spec.Images = make(map[string]appsv1alpha1.ImageSpec, 1)
}
imageSpec := nodeImage.Spec.Images[imageName]
imageSpec.SandboxConfig = job.Spec.SandboxConfig

for _, secret := range secrets {
if !containsObject(imageSpec.PullSecrets, secret) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/daemon/criruntime/imageruntime/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
"io"
"net"
"net/http"
Expand Down Expand Up @@ -85,7 +86,7 @@ type containerdImageClient struct {
}

// PullImage implements ImageService.PullImage.
func (d *containerdImageClient) PullImage(ctx context.Context, imageName, tag string, pullSecrets []v1.Secret) (ImagePullStatusReader, error) {
func (d *containerdImageClient) PullImage(ctx context.Context, imageName, tag string, pullSecrets []v1.Secret, _ *appsv1alpha1.SandboxConfig) (ImagePullStatusReader, error) {
ctx = namespaces.WithNamespace(ctx, k8sContainerdNamespace)

if tag == "" {
Expand Down
9 changes: 8 additions & 1 deletion pkg/daemon/criruntime/imageruntime/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package imageruntime

import (
"context"
appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
"io"
"time"

Expand Down Expand Up @@ -66,7 +67,7 @@ type commonCRIImageService struct {
}

// PullImage implements ImageService.PullImage.
func (c *commonCRIImageService) PullImage(ctx context.Context, imageName, tag string, pullSecrets []v1.Secret) (ImagePullStatusReader, error) {
func (c *commonCRIImageService) PullImage(ctx context.Context, imageName, tag string, pullSecrets []v1.Secret, sandboxConfig *appsv1alpha1.SandboxConfig) (ImagePullStatusReader, error) {
registry := daemonutil.ParseRegistry(imageName)
fullImageName := imageName + ":" + tag
// Reader
Expand All @@ -81,6 +82,12 @@ func (c *commonCRIImageService) PullImage(ctx context.Context, imageName, tag st
},
Auth: auth, //default is nil
}
if sandboxConfig != nil {
pullImageReq.SandboxConfig = &runtimeapi.PodSandboxConfig{
Annotations: sandboxConfig.Annotations,
Labels: sandboxConfig.Labels,
}
}
var err error
if len(pullSecrets) > 0 {
var authInfos []daemonutil.AuthInfo
Expand Down
3 changes: 2 additions & 1 deletion pkg/daemon/criruntime/imageruntime/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package imageruntime

import (
"context"
appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
"io"
"sync"

Expand Down Expand Up @@ -68,7 +69,7 @@ func (d *dockerImageService) handleRuntimeError(err error) {
}
}

func (d *dockerImageService) PullImage(ctx context.Context, imageName, tag string, pullSecrets []v1.Secret) (reader ImagePullStatusReader, err error) {
func (d *dockerImageService) PullImage(ctx context.Context, imageName, tag string, pullSecrets []v1.Secret, _ *appsv1alpha1.SandboxConfig) (reader ImagePullStatusReader, err error) {
if err = d.createRuntimeClientIfNecessary(); err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/daemon/criruntime/imageruntime/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package imageruntime

import (
"context"
appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"

v1 "k8s.io/api/core/v1"
)
Expand Down Expand Up @@ -46,6 +47,6 @@ type ImagePullStatusReader interface {
}

type ImageService interface {
PullImage(ctx context.Context, imageName, tag string, pullSecrets []v1.Secret) (ImagePullStatusReader, error)
PullImage(ctx context.Context, imageName, tag string, pullSecrets []v1.Secret, sandboxConfig *appsv1alpha1.SandboxConfig) (ImagePullStatusReader, error)
ListImages(ctx context.Context) ([]ImageInfo, error)
}
3 changes: 2 additions & 1 deletion pkg/daemon/criruntime/imageruntime/pouch.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package imageruntime
import (
"context"
"fmt"
appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
"io"
"sync"

Expand Down Expand Up @@ -70,7 +71,7 @@ func (d *pouchImageService) handleRuntimeError(err error) {
}
}

func (d *pouchImageService) PullImage(ctx context.Context, imageName, tag string, pullSecrets []v1.Secret) (reader ImagePullStatusReader, err error) {
func (d *pouchImageService) PullImage(ctx context.Context, imageName, tag string, pullSecrets []v1.Secret, _ *appsv1alpha1.SandboxConfig) (reader ImagePullStatusReader, err error) {
if err = d.createRuntimeClientIfNecessary(); err != nil {
return nil, err
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/daemon/imagepuller/imagepuller_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (w *realWorkerPool) Sync(spec *appsv1alpha1.ImageSpec, status *appsv1alpha1
_, ok := w.pullWorkers[tagSpec.Tag]

if !ok {
worker := newPullWorker(w.name, tagSpec, secrets, w.runtime, w, ref, w.eventRecorder)
worker := newPullWorker(w.name, tagSpec, spec.SandboxConfig, secrets, w.runtime, w, ref, w.eventRecorder)
w.pullWorkers[tagSpec.Tag] = worker
}
}
Expand Down Expand Up @@ -262,10 +262,11 @@ func (w *realWorkerPool) UpdateStatus(status *appsv1alpha1.ImageTagStatus) {
w.tagStatuses[status.Tag] = status
}

func newPullWorker(name string, tagSpec appsv1alpha1.ImageTagSpec, secrets []v1.Secret, runtime runtimeimage.ImageService, statusUpdater imageStatusUpdater, ref *v1.ObjectReference, eventRecorder record.EventRecorder) *pullWorker {
func newPullWorker(name string, tagSpec appsv1alpha1.ImageTagSpec, sandboxConfig *appsv1alpha1.SandboxConfig, secrets []v1.Secret, runtime runtimeimage.ImageService, statusUpdater imageStatusUpdater, ref *v1.ObjectReference, eventRecorder record.EventRecorder) *pullWorker {
o := &pullWorker{
name: name,
tagSpec: tagSpec,
sandboxConfig: sandboxConfig,
secrets: secrets,
runtime: runtime,
statusUpdater: statusUpdater,
Expand All @@ -283,6 +284,7 @@ type pullWorker struct {

name string
tagSpec appsv1alpha1.ImageTagSpec
sandboxConfig *appsv1alpha1.SandboxConfig
secrets []v1.Secret
runtime runtimeimage.ImageService
statusUpdater imageStatusUpdater
Expand Down Expand Up @@ -435,7 +437,7 @@ func (w *pullWorker) doPullImage(ctx context.Context, newStatus *appsv1alpha1.Im
var statusReader runtimeimage.ImagePullStatusReader
pullChan := make(chan struct{})
go func() {
statusReader, err = w.runtime.PullImage(ctx, w.name, tag, w.secrets)
statusReader, err = w.runtime.PullImage(ctx, w.name, tag, w.secrets, w.sandboxConfig)
close(pullChan)
}()

Expand Down

0 comments on commit 36c7ef4

Please sign in to comment.