Skip to content

Commit

Permalink
fix: add support for http mirror (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
hysyeah authored Nov 20, 2024
1 parent 70f6578 commit 28ab81f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pkg/apiserver/handler_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ func (h *Handler) cronWorkflowMutate(ctx context.Context, req *admissionv1.Admis
if err != nil {
continue
}
newImage := utils.ReplacedImageRef(mirrorsEndpoint, ref.String(), false)
newImage, _ := utils.ReplacedImageRef(mirrorsEndpoint, ref.String(), false)
wf.Spec.WorkflowSpec.Templates[i].Container.Image = newImage
}
original := req.Object.Raw
Expand Down
11 changes: 7 additions & 4 deletions pkg/images/puller.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ func (is *imageService) PullImage(ctx context.Context, ref appv1alpha1.Ref, opts
return ref.Name, err
}
ref.Name = originNamed.String()
config := newFetchConfig()
// replaced image ref
replacedRef := utils.ReplacedImageRef(mirrorsEndpoint, originNamed.String(), true)
replacedRef, plainHTTP := utils.ReplacedImageRef(mirrorsEndpoint, originNamed.String(), true)
config := newFetchConfig(plainHTTP)

ongoing := newJobs(replacedRef, originNamed.String())

Expand Down Expand Up @@ -104,6 +104,7 @@ func (is *imageService) PullImage(ctx context.Context, ref appv1alpha1.Ref, opts
if config.AllMetadata {
remoteOpts = append(remoteOpts, containerd.WithAllMetadata())
}

if config.PlatformMatcher != nil {
remoteOpts = append(remoteOpts, containerd.WithPlatformMatcher(config.PlatformMatcher))
} else {
Expand Down Expand Up @@ -233,8 +234,10 @@ func shouldPUllImage(ref appv1alpha1.Ref, imagePresent bool) bool {
return false
}

func newFetchConfig() *content.FetchConfig {
options := docker.ResolverOptions{}
func newFetchConfig(plainHTTP bool) *content.FetchConfig {
options := docker.ResolverOptions{
PlainHTTP: plainHTTP,
}
resolver := docker.NewResolver(options)
config := &content.FetchConfig{
Resolver: resolver,
Expand Down
18 changes: 12 additions & 6 deletions pkg/utils/k8sutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,23 +131,29 @@ func GetMirrorsEndpoint() (ep []string) {
return ep
}

func ReplacedImageRef(mirrorsEndpoint []string, oldImageRef string, checkConnection bool) string {
// ReplacedImageRef return replaced image ref and true if mirror is support http
func ReplacedImageRef(mirrorsEndpoint []string, oldImageRef string, checkConnection bool) (string, bool) {
if len(mirrorsEndpoint) == 0 {
return oldImageRef
return oldImageRef, false
}
plainHTTP := false
for _, ep := range mirrorsEndpoint {
if ep != "" && ep != DefaultRegistry {
url, err := url.Parse(ep)
if err != nil {
continue
}
if url.Scheme == "http" {
continue
plainHTTP = true
}
if checkConnection {
host := url.Host
if !hasPort(url.Host) {
host = net.JoinHostPort(url.Host, "443")
if url.Scheme == "https" {
host = net.JoinHostPort(url.Host, "443")
} else {
host = net.JoinHostPort(url.Host, "80")
}
}
conn, err := net.DialTimeout("tcp", host, 2*time.Second)
if err != nil {
Expand All @@ -162,10 +168,10 @@ func ReplacedImageRef(mirrorsEndpoint []string, oldImageRef string, checkConnect
klog.Infof("parts: %s", parts)
parts[0] = url.Host
klog.Infof("parts2: %s", parts)
return strings.Join(parts, "/")
return strings.Join(parts, "/"), plainHTTP
}
}
return oldImageRef
return oldImageRef, false
}

func hasPort(s string) bool { return strings.LastIndex(s, ":") > strings.LastIndex(s, "]") }

0 comments on commit 28ab81f

Please sign in to comment.