mirror of
https://github.com/zitadel/zitadel.git
synced 2025-12-03 07:42:20 +00:00
feat: enable running ZITADEL offline (#1894)
* feat: enable running ZITADEL offline * refactor: move operator image to common images * test: remove empty test
This commit is contained in:
46
operator/common/images.go
Normal file
46
operator/common/images.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package common
|
||||
|
||||
type image string
|
||||
|
||||
func (i image) String() string { return string(i) }
|
||||
|
||||
type dockerhubImage image
|
||||
|
||||
type zitadelImage image
|
||||
|
||||
const (
|
||||
CockroachImage dockerhubImage = "cockroachdb/cockroach:v20.2.3"
|
||||
PostgresImage dockerhubImage = "postgres:9.6.17"
|
||||
FlywayImage dockerhubImage = "flyway/flyway:7.5.1"
|
||||
AlpineImage dockerhubImage = "alpine:3.11"
|
||||
ZITADELImage zitadelImage = "caos/zitadel"
|
||||
BackupImage zitadelImage = "caos/zitadel-crbackup"
|
||||
ZITADELOperatorImage zitadelImage = "caos/zitadel-operator"
|
||||
)
|
||||
|
||||
func (z zitadelImage) Reference(customImageRegistry, version string) string {
|
||||
|
||||
reg := "ghcr.io"
|
||||
if customImageRegistry != "" {
|
||||
reg = customImageRegistry
|
||||
}
|
||||
|
||||
return concat(image(z), reg, version)
|
||||
}
|
||||
|
||||
func (d dockerhubImage) Reference(customImageRegistry string) string {
|
||||
return concat(image(d), customImageRegistry, "")
|
||||
}
|
||||
|
||||
func concat(img image, customImageRegistry, version string) string {
|
||||
str := img.String()
|
||||
|
||||
if customImageRegistry != "" {
|
||||
str = customImageRegistry + "/" + str
|
||||
}
|
||||
|
||||
if version != "" {
|
||||
str = str + ":" + version
|
||||
}
|
||||
return str
|
||||
}
|
||||
106
operator/common/images_test.go
Normal file
106
operator/common/images_test.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDockerHubReference(t *testing.T) {
|
||||
|
||||
imgs := []dockerhubImage{CockroachImage, PostgresImage, FlywayImage, AlpineImage}
|
||||
|
||||
type args struct {
|
||||
customImageRegistry string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
test func(result string) error
|
||||
}{{
|
||||
name: "Image should be pulled from docker hub by default",
|
||||
args: args{
|
||||
customImageRegistry: "",
|
||||
},
|
||||
test: func(result string) error {
|
||||
return expectRegistry(result, "")
|
||||
},
|
||||
}, {
|
||||
name: "Given a custom image registry, the registry should be prepended",
|
||||
args: args{
|
||||
customImageRegistry: "myreg.io",
|
||||
},
|
||||
test: func(result string) error {
|
||||
return expectRegistry(result, "myreg.io")
|
||||
},
|
||||
}}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
for i := range imgs {
|
||||
got := imgs[i].Reference(tt.args.customImageRegistry)
|
||||
if err := tt.test(got); err != nil {
|
||||
t.Error(fmt.Errorf("DockerHubReference(%s): %w", imgs[i], err))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestZITADELReference(t *testing.T) {
|
||||
|
||||
imgs := []zitadelImage{ZITADELImage, BackupImage}
|
||||
dummyVersion := "v99.99.99"
|
||||
|
||||
type args struct {
|
||||
customImageRegistry string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
test func(result string) error
|
||||
}{{
|
||||
name: "Image should be pulled from GHCR by default",
|
||||
args: args{
|
||||
customImageRegistry: "",
|
||||
},
|
||||
test: func(result string) error {
|
||||
return expectRegistry(result, "ghcr.io/")
|
||||
},
|
||||
}, {
|
||||
name: "Given a random docker hub image and a custom image registry, the registry should be prepended",
|
||||
args: args{
|
||||
customImageRegistry: "myreg.io",
|
||||
},
|
||||
test: func(result string) error {
|
||||
return expectRegistry(result, "myreg.io")
|
||||
},
|
||||
}}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
for i := range imgs {
|
||||
got := imgs[i].Reference(tt.args.customImageRegistry, dummyVersion)
|
||||
if err := tt.test(got); err != nil {
|
||||
t.Error(fmt.Errorf("ZITADELReference(%s): %w", imgs[i], err))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func expectRegistry(result, expect string) error {
|
||||
if !strings.HasPrefix(result, expect) {
|
||||
return fmt.Errorf("image is not prefixed by the registry %s", expect)
|
||||
}
|
||||
points := strings.Count(result[:strings.Index(result, ":")], ".")
|
||||
if expect == "" && points > 1 {
|
||||
return errors.New("doesn't look like a docker image")
|
||||
}
|
||||
|
||||
if points > 1 {
|
||||
return errors.New("too many points")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user