Skip to content

Commit

Permalink
Registry in image widget
Browse files Browse the repository at this point in the history
  • Loading branch information
dzsak committed Mar 18, 2024
1 parent a635600 commit be69bce
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 24 deletions.
33 changes: 18 additions & 15 deletions cmd/agent/imagebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func dockerfileImageBuild(
reqUrl := fmt.Sprintf("%s/agent/imagebuild/%s", gimletHost, buildId)
jobName := fmt.Sprintf("kaniko-%d", rand.Uint32())
job := generateJob(trigger, jobName, reqUrl)
job = mountPushSecret(job, trigger.Image)
job = mountPushSecret(job, trigger.Registry)
_, err := kubeEnv.Client.BatchV1().Jobs("infrastructure").Create(context.TODO(), job, meta_v1.CreateOptions{})
if err != nil {
logrus.Errorf("cannot apply job: %s", err)
Expand Down Expand Up @@ -354,9 +354,9 @@ func generateJob(trigger dx.ImageBuildRequest, name, sourceUrl string) *batchv1.
}
}

func mountPushSecret(job *batchv1.Job, image string) *batchv1.Job {
func mountPushSecret(job *batchv1.Job, registry string) *batchv1.Job {
optional := true
if strings.HasPrefix(image, "ghcr.io") {
if registry == "ghcrRegistry" {
job.Spec.Template.Spec.Containers[0].VolumeMounts = append(job.Spec.Template.Spec.Containers[0].VolumeMounts, corev1.VolumeMount{
MountPath: "/kaniko/.docker",
Name: "ghcr-registry-pushsecret",
Expand All @@ -374,20 +374,23 @@ func mountPushSecret(job *batchv1.Job, image string) *batchv1.Job {
return job
}

job.Spec.Template.Spec.Containers[0].VolumeMounts = append(job.Spec.Template.Spec.Containers[0].VolumeMounts, corev1.VolumeMount{
MountPath: "/kaniko/.docker",
Name: "dockerhub-registry-pushsecret",
})
if registry == "dockerhubRegistry" {
job.Spec.Template.Spec.Containers[0].VolumeMounts = append(job.Spec.Template.Spec.Containers[0].VolumeMounts, corev1.VolumeMount{
MountPath: "/kaniko/.docker",
Name: "dockerhub-registry-pushsecret",
})

job.Spec.Template.Spec.Volumes = append(job.Spec.Template.Spec.Volumes, corev1.Volume{
Name: "dockerhub-registry-pushsecret",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: "dockerhub-registry-pushsecret",
Optional: &optional,
job.Spec.Template.Spec.Volumes = append(job.Spec.Template.Spec.Volumes, corev1.Volume{
Name: "dockerhub-registry-pushsecret",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: "dockerhub-registry-pushsecret",
Optional: &optional,
},
},
},
})
})
return job
}
return job
}

Expand Down
9 changes: 6 additions & 3 deletions pkg/dashboard/gitops/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,11 +395,11 @@ func ExtractImageStrategy(envConfig *dx.Manifest) string {
return "dynamic"
}

func ExtractImageRepoTagAndDockerfile(envConfig *dx.Manifest, vars map[string]string) (string, string, string) {
func ExtractImageRepoTagDockerfileAndRegistry(envConfig *dx.Manifest, vars map[string]string) (string, string, string, string) {
envConfig.ResolveVars(vars)
image := envConfig.Values["image"]

var repository, tag, dockerfile string
var repository, tag, dockerfile, registry string
if image != nil {
imageMap := image.(map[string]interface{})

Expand All @@ -412,7 +412,10 @@ func ExtractImageRepoTagAndDockerfile(envConfig *dx.Manifest, vars map[string]st
if val, ok := imageMap["dockerfile"]; ok {
dockerfile = val.(string)
}
if val, ok := imageMap["registry"]; ok {
registry = val.(string)
}
}

return repository, tag, dockerfile
return repository, tag, dockerfile, registry
}
3 changes: 2 additions & 1 deletion pkg/dashboard/server/releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func release(w http.ResponseWriter, r *http.Request) {

vars := artifact.CollectVariables()
vars["APP"] = releaseRequest.App
imageRepository, imageTag, dockerfile := gitops.ExtractImageRepoTagAndDockerfile(manifest, vars)
imageRepository, imageTag, dockerfile, registry := gitops.ExtractImageRepoTagDockerfileAndRegistry(manifest, vars)
// Image push happens inside the cluster, pull is handled by the kubelet that doesn't speak cluster local addresses
imageRepository = strings.ReplaceAll(imageRepository, "127.0.0.1:32447", "registry.infrastructure.svc.cluster.local:5000")
imageBuildRequest = &dx.ImageBuildRequest{
Expand All @@ -295,6 +295,7 @@ func release(w http.ResponseWriter, r *http.Request) {
Image: imageRepository,
Tag: imageTag,
Dockerfile: dockerfile,
Registry: registry,
}
break
}
Expand Down
1 change: 1 addition & 0 deletions pkg/dx/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type ImageBuildRequest struct {
Tag string `json:"tag"`
SourcePath string `json:"sourcePath"`
Dockerfile string `json:"dockerfile"`
Registry string `json:"registry"`
}

// RollbackRequest contains all metadata about the rollback intent
Expand Down
1 change: 1 addition & 0 deletions web/dashboard/src/views/envConfig/envConfig.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class EnvConfig extends Component {
repository: "127.0.0.1:32447/"+config,
tag: "{{ .SHA }}",
strategy: "buildpacks",
registry: "builtInRegistry",
},
resources: {
ignoreLimits: true,
Expand Down
56 changes: 51 additions & 5 deletions web/dashboard/src/views/envConfig/imageWidget.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ImageWidget extends Component {
let repository = ""
let tag = ""
let dockerfile = ""
let registry = "builtInRegistry"

switch (strategy) {
case 'dynamic':
Expand Down Expand Up @@ -43,6 +44,7 @@ class ImageWidget extends Component {
repository: repository,
tag: tag,
dockerfile: dockerfile,
registry: registry,
}
}

Expand All @@ -52,13 +54,37 @@ class ImageWidget extends Component {
{
[name]: event.target.value,
},
() => this.props.onChange({"repository": this.state.repository, "tag": this.state.tag, "dockerfile": this.state.dockerfile, "strategy": this.state.strategy})
() => this.props.onChange({"repository": this.state.repository, "tag": this.state.tag, "dockerfile": this.state.dockerfile, "strategy": this.state.strategy, "registry": this.state.registry })
);
};
}

selectOnChange(value) {
const { registry } = this.props;
let repository = "";
const login = registry[value].login ?? "your-company"

switch (value) {
case 'dockerhubRegistry':
repository = `${login}/{{ .APP }}`
break;
case 'ghcrRegistry':
repository = `ghcr.io/${login}/{{ .APP }}`
break;
default:
repository = "127.0.0.1:32447/{{ .APP }}"
}

this.props.onChange({"repository": repository, "tag": this.state.tag, "dockerfile": this.state.dockerfile, "strategy": this.state.strategy, "registry": value})
this.setState({ registry: value });
}

render() {
const { strategy, repository, tag, dockerfile } = this.state;
const { registry } = this.props;

console.log(this.state)

return (
<>
<div className="form-group field field-object">
Expand All @@ -68,7 +94,7 @@ class ImageWidget extends Component {
<div className="mt-4 grid grid-cols-1 gap-y-6 sm:grid-cols-4 sm:gap-x-4 px-2">
<div
className={`relative flex cursor-pointer rounded-lg border bg-white p-4 shadow-sm focus:outline-none ${strategy === "static" ? "border-indigo-600" : ""}`}
onClick={(e) => this.setState({strategy: "static", ...this.defaults("static")}, () => this.props.onChange({"repository": this.state.repository, "tag": this.state.tag, "strategy": this.state.strategy}))}
onClick={(e) => this.setState({strategy: "static", ...this.defaults("static")}, () => this.props.onChange({"repository": this.state.repository, "tag": this.state.tag, "strategy": this.state.strategy, "registry": this.state.registry}))}
>
<span className="flex flex-1">
<span className="flex flex-col">
Expand All @@ -83,7 +109,7 @@ class ImageWidget extends Component {

<div
className={`relative flex cursor-pointer rounded-lg border bg-white p-4 shadow-sm focus:outline-none ${strategy === "dynamic" ? "border-indigo-600" : ""}`}
onClick={(e) => this.setState({strategy: "dynamic", ...this.defaults("dynamic")}, () => this.props.onChange({"repository": this.state.repository, "tag": this.state.tag, "strategy": this.state.strategy}))}
onClick={(e) => this.setState({strategy: "dynamic", ...this.defaults("dynamic")}, () => this.props.onChange({"repository": this.state.repository, "tag": this.state.tag, "strategy": this.state.strategy, "registry": this.state.registry}))}
>
<span className="flex flex-1">
<span className="flex flex-col">
Expand All @@ -98,7 +124,7 @@ class ImageWidget extends Component {

<div
className={`relative pr-8 flex cursor-pointer rounded-lg border bg-white p-4 shadow-sm focus:outline-none ${strategy === "buildpacks" ? "border-indigo-600" : ""}`}
onClick={(e) => this.setState({strategy: "buildpacks", ...this.defaults("buildpacks")}, () => this.props.onChange({"repository": this.state.repository, "tag": this.state.tag, "strategy": this.state.strategy}))}
onClick={(e) => this.setState({strategy: "buildpacks", ...this.defaults("buildpacks")}, () => this.props.onChange({"repository": this.state.repository, "tag": this.state.tag, "strategy": this.state.strategy, "registry": this.state.registry}))}
>
<span className="flex flex-1">
<span className="flex flex-col">
Expand All @@ -113,7 +139,7 @@ class ImageWidget extends Component {

<div
className={`relative pr-8 flex cursor-pointer rounded-lg border bg-white p-4 shadow-sm focus:outline-none ${strategy === "dockerfile" ? "border-indigo-600" : ""}`}
onClick={(e) => this.setState({strategy: "dockerfile", ...this.defaults("dockerfile")}, () => this.props.onChange({"repository": this.state.repository, "tag": this.state.tag, "dockerfile": this.state.dockerfile, "strategy": this.state.strategy}))}
onClick={(e) => this.setState({strategy: "dockerfile", ...this.defaults("dockerfile")}, () => this.props.onChange({"repository": this.state.repository, "tag": this.state.tag, "dockerfile": this.state.dockerfile, "strategy": this.state.strategy, "registry": this.state.registry}))}
>
<span className="flex flex-1">
<span className="flex flex-col">
Expand Down Expand Up @@ -141,6 +167,26 @@ class ImageWidget extends Component {
<input className="form-control" id="root_tag" label="Dockerfile" required="" placeholder="" type="text" list="examples_root_tag" value={dockerfile} onChange={this.onChange('dockerfile')}/>
</div>
}
{(strategy === "dockerfile" || strategy === "buildpacks") &&
<div className="form-group field">
<label className="control-label" htmlFor="root_tag">Registry<span className="required">*</span></label>
<select id="root_1__anyof_select" className="form-control" value={this.state.registry} onChange={e => this.selectOnChange(e.target.value)}>
{
Object.keys(registry).map((item, idx) => {
if (!registry[item].hasOwnProperty("enabled")) {
return null
}

if (!registry[item].enabled) {
return null
}

return (<option key={idx} value={item}>{item}</option>)
})
}
</select>
</div>
}
</fieldset>
</div>
</>
Expand Down

0 comments on commit be69bce

Please sign in to comment.