mirror of
https://github.com/kubevela/kubevela.git
synced 2026-03-05 19:22:03 +00:00
* Feat: check existence of an image Checked existence of an image in - public docker hub - private image registry Add an API to retrieve image registry secrets by an image and or its tag. In veluaUX, it will help auto-fill uesrs' imagePullSecrets fields Fix https://github.com/kubevela/velaux/issues/436 Signed-off-by: Zheng Xi Zhou <zzxwill@gmail.com> * replace the implementation with libary google/go-containerregistry Signed-off-by: Zheng Xi Zhou <zzxwill@gmail.com> * address comments Signed-off-by: Zheng Xi Zhou <zzxwill@gmail.com> * add more testcases Signed-off-by: Zheng Xi Zhou <zzxwill@gmail.com> * fix UT Signed-off-by: Zheng Xi Zhou <zzxwill@gmail.com>
90 lines
2.0 KiB
Go
90 lines
2.0 KiB
Go
/*
|
|
Copyright 2022 The KubeVela 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 image
|
|
|
|
import (
|
|
"github.com/google/go-containerregistry/pkg/authn"
|
|
"github.com/google/go-containerregistry/pkg/name"
|
|
v1 "github.com/google/go-containerregistry/pkg/v1"
|
|
"github.com/google/go-containerregistry/pkg/v1/remote"
|
|
)
|
|
|
|
// Meta is the struct for image metadata
|
|
type Meta struct {
|
|
Registry string
|
|
Repository string
|
|
Name string
|
|
Tag string
|
|
}
|
|
|
|
// DockerHubImageTagResponse is the struct for docker hub image tag response
|
|
type DockerHubImageTagResponse struct {
|
|
Count int `json:"count"`
|
|
Results []Result
|
|
}
|
|
|
|
// Result is the struct for docker hub image tag result
|
|
type Result struct {
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
// IsExisted checks whether a public or private image exists
|
|
func IsExisted(username, password, image string) (bool, error) {
|
|
ref, err := name.ParseReference(image)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
var img v1.Image
|
|
|
|
if username != "" || password != "" {
|
|
basic := &authn.Basic{
|
|
Username: username,
|
|
Password: password,
|
|
}
|
|
|
|
option := remote.WithAuth(basic)
|
|
img, err = remote.Image(ref, option)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
} else {
|
|
img, err = remote.Image(ref)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
}
|
|
|
|
_, err = img.Digest()
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
_, err = img.Manifest()
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return true, nil
|
|
}
|
|
|
|
// RegistryMeta is the struct for registry metadata
|
|
type RegistryMeta struct {
|
|
Username string `json:"username"`
|
|
Password string `json:"password"`
|
|
}
|