Skip to content

Commit e8c1071

Browse files
committed
feat: added support for release imagestream images
added the ability to pass in an imagestream to initialize utility map for remote pullspecs, if no imageregistry is present, any existing pullspec will be updated to refer to the remote pullspec update: use new methods to help replace pullspec used new methods to help with asset files and updated disruption deployment to use pullspec method Signed-off-by: ehila <[email protected]> feat: added panic method option for pullspec Signed-off-by: ehila <[email protected]>
1 parent 2940e3b commit e8c1071

File tree

3 files changed

+96
-2
lines changed

3 files changed

+96
-2
lines changed

pkg/clioptions/clusterdiscovery/provider.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/onsi/ginkgo/v2"
99
"github.com/onsi/gomega"
1010

11+
"github.com/openshift/origin/test/extended/util/image"
1112
e2e "k8s.io/kubernetes/test/e2e/framework"
1213

1314
exutil "github.com/openshift/origin/test/extended/util"
@@ -67,6 +68,16 @@ func InitializeTestFramework(context *e2e.TestContextType, config *ClusterConfig
6768

6869
// IPFamily constants are taken from kube e2e and used by tests
6970
context.IPFamily = config.IPFamily
71+
72+
imageStreamString, _, err := exutil.NewCLIWithoutNamespace("").AsAdmin().Run("adm", "release", "info", `-ojsonpath={.references}`).Outputs()
73+
if err != nil {
74+
return err
75+
}
76+
77+
if err := image.InitializeReleasePullSpecString(imageStreamString, config.HasNoOptionalCapabilities); err != nil {
78+
return err
79+
}
80+
7081
return nil
7182
}
7283

pkg/monitortests/network/disruptionpodnetwork/monitortest.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ func (pna *podNetworkAvalibility) StartCollection(ctx context.Context, adminREST
178178
pna.targetService = service
179179

180180
hostNetworkTargetDeployment.Spec.Replicas = &numNodes
181+
hostNetworkTargetDeployment.Spec.Template.Spec.Containers[0].Image = image.LimitedShellImage()
181182
if _, err := pna.kubeClient.AppsV1().Deployments(pna.namespaceName).Create(context.Background(), hostNetworkTargetDeployment, metav1.CreateOptions{}); err != nil {
182183
return err
183184
}

test/extended/util/image/image.go

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package image
33
import (
44
"crypto/sha256"
55
"encoding/base64"
6+
"encoding/json"
67
"fmt"
78
"regexp"
89
"strings"
910
"sync"
1011
"time"
1112

13+
imageapi "github.com/openshift/api/image/v1"
1214
"k8s.io/apimachinery/pkg/util/sets"
1315
k8simage "k8s.io/kubernetes/test/utils/image"
1416
)
@@ -19,6 +21,14 @@ var (
1921
fromRepository string
2022
images map[string]string
2123

24+
releasePullSpecInitializationLock sync.RWMutex
25+
releasePullSpecInitialized bool
26+
availablePullSpecs = map[string]string{
27+
"cli": "image-registry.openshift-image-registry.svc:5000/openshift/cli:latest",
28+
"tools": "image-registry.openshift-image-registry.svc:5000/openshift/tools:latest",
29+
}
30+
imageRegistryPullSpecRegex = regexp.MustCompile(`image-registry\.openshift-image-registry\.svc:5000\/openshift\/([A-Za-z0-9._-]+)[@:A-Za-z0-9._-]*`)
31+
2232
allowedImages = map[string]k8simage.ImageID{
2333
// used by jenkins tests
2434
"quay.io/redhat-developer/nfs-server:1.1": -1,
@@ -106,6 +116,27 @@ func ReplaceContents(data []byte) ([]byte, error) {
106116
patterns[to] = re
107117
}
108118

119+
// find any references to image-registry.openshift-image-registry.svc:5000/openshift/<image>:<tag>
120+
// append pattern match for appropriate PullSpec if one exists
121+
allMatches := imageRegistryPullSpecRegex.FindAllStringSubmatch(string(data), -1)
122+
for _, match := range allMatches {
123+
if len(match) != 2 {
124+
continue
125+
}
126+
exactMatchedPullSpec := match[0]
127+
tagMatchedPullSpec := match[1]
128+
to, found := GetPullSpecFor(tagMatchedPullSpec)
129+
if !found || to == exactMatchedPullSpec {
130+
continue
131+
}
132+
pattern := fmt.Sprintf(exactImageFormat, regexp.QuoteMeta(exactMatchedPullSpec))
133+
re, err := regexp.Compile(pattern)
134+
if err != nil {
135+
return nil, err
136+
}
137+
patterns[to] = re
138+
}
139+
109140
for to, pattern := range patterns {
110141
data = pattern.ReplaceAll(data, []byte(to))
111142
}
@@ -146,15 +177,15 @@ the test/extended/util/image/README.md file.`, image))
146177
// to avoid the need to add packages by using simpler concepts or consider
147178
// extending an existing image.
148179
func ShellImage() string {
149-
return "image-registry.openshift-image-registry.svc:5000/openshift/tools:latest"
180+
return GetPullSpecForOrPanic("tools")
150181
}
151182

152183
// LimitedShellImage returns a docker pull spec that any pod on the cluster
153184
// has access to that contains bash and standard commandline tools.
154185
// This image should be used when you only need oc and can't use the shell image.
155186
// This image has oc.
156187
func LimitedShellImage() string {
157-
return "image-registry.openshift-image-registry.svc:5000/openshift/cli:latest"
188+
return GetPullSpecForOrPanic("cli")
158189
}
159190

160191
// OpenLDAPTestImage returns the LDAP test image.
@@ -236,3 +267,54 @@ func GetMappedImages(originalImages map[string]k8simage.ImageID, repo string) ma
236267
}
237268
return configs
238269
}
270+
271+
func GetPullSpecFor(image string) (string, bool) {
272+
if spec, found := availablePullSpecs[image]; found {
273+
return spec, true
274+
}
275+
return "", false
276+
}
277+
278+
func GetPullSpecForOrPanic(image string) string {
279+
spec, found := GetPullSpecFor(image)
280+
if !found {
281+
panic(fmt.Sprintf("could not find PullSpec for (%s)", image))
282+
}
283+
return spec
284+
}
285+
286+
// InitializeReleasePullSpecString initializes a mapping with the PullSpec ImageStream string from
287+
// the release payload
288+
//
289+
// When running in clusters that do not have ImageRegistry installed it is necessary to use the full
290+
// PullSpec for ImageRegistry containers such as `cli` or `tools`, Pass in an imageStreamString of
291+
// the discovered PullSpec (ex. oc adm release info `-ojsonpath='{.references}'). This method will then
292+
// initialize the package with the PullSpec for that release.
293+
//
294+
// Using GetPullSpecFor(), ShellImage() and LimitedShellImage() will return the appropriate
295+
// PullSpec, either the ReleasePayload or ImageRegistry.
296+
func InitializeReleasePullSpecString(imageStreamString string, hasNoImageRegistry bool) error {
297+
releasePullSpecInitializationLock.Lock()
298+
defer releasePullSpecInitializationLock.Unlock()
299+
if releasePullSpecInitialized {
300+
panic("attempt to double initialize ReleasePullSpec")
301+
}
302+
images := &imageapi.ImageStream{}
303+
err := json.Unmarshal([]byte(imageStreamString), images)
304+
if err != nil {
305+
return fmt.Errorf("failed to unmarshal release ImageStream string: %w", err)
306+
}
307+
308+
for _, tag := range images.Spec.Tags {
309+
if tag.From.Kind != "DockerImage" {
310+
continue
311+
}
312+
// If an available pullthrough spec is already defined, and we do have an ImageRegistry
313+
// we leave it alone
314+
if _, available := availablePullSpecs[tag.Name]; available && !hasNoImageRegistry {
315+
continue
316+
}
317+
availablePullSpecs[tag.Name] = tag.From.Name
318+
}
319+
return nil
320+
}

0 commit comments

Comments
 (0)