Skip to content

Commit

Permalink
Image widget edit
Browse files Browse the repository at this point in the history
  • Loading branch information
dzsak committed Mar 20, 2024
1 parent 9b7e94e commit 6b8a592
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 88 deletions.
2 changes: 1 addition & 1 deletion cmd/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func serverCommunication(
var imageBuildRequest dx.ImageBuildRequest
_ = json.Unmarshal(requestString, &imageBuildRequest)

if imageBuildRequest.Dockerfile != "" {
if imageBuildRequest.Strategy == "dockerfile" {
go dockerfileImageBuild(kubeEnv, gimletHost, buildId, imageBuildRequest, messages)
} else {
go buildImage(gimletHost, agentKey, buildId, imageBuildRequest, messages, config.ImageBuilderHost)
Expand Down
36 changes: 21 additions & 15 deletions pkg/dashboard/server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,28 +301,17 @@ func stackConfig(w http.ResponseWriter, r *http.Request) {
return
}

var stackConfig *dx.StackConfig
stackYamlPath := "stack.yaml"
if !env.RepoPerEnv {
stackYamlPath = filepath.Join(env.Name, "stack.yaml")
}

gitRepoCache, _ := r.Context().Value("gitRepoCache").(*nativeGit.RepoCache)
err = gitRepoCache.PerformAction(env.InfraRepo, func(repo *git.Repository) error {
var inerErr error
stackConfig, inerErr = stackYaml(repo, stackYamlPath)
return inerErr
})
stackConfig, err := StackConfig(gitRepoCache, stackYamlPath, env.InfraRepo)
if err != nil {
if !strings.Contains(err.Error(), "file not found") {
logrus.Errorf("cannot get stack yaml from repo: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
} else {
logrus.Errorf("cannot get repo: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
logrus.Errorf("cannot get stack config: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}

stackDefinition, err := loadStackDefinition(stackConfig)
Expand All @@ -349,6 +338,23 @@ func stackConfig(w http.ResponseWriter, r *http.Request) {
w.Write(gitopsEnvString)
}

func StackConfig(gitRepoCache *nativeGit.RepoCache, stackYamlPath, infraRepo string) (*dx.StackConfig, error) {
var stackConfig *dx.StackConfig
err := gitRepoCache.PerformAction(infraRepo, func(repo *git.Repository) error {
var inerErr error
stackConfig, inerErr = stackYaml(repo, stackYamlPath)
return inerErr
})
if err != nil {
if !strings.Contains(err.Error(), "file not found") {
return nil, fmt.Errorf("cannot get stack yaml from repo: %s", err)
} else {
return nil, fmt.Errorf("cannot get repo: %s", err)
}
}
return stackConfig, nil
}

func loadStackDefinition(stackConfig *dx.StackConfig) (map[string]interface{}, error) {
var url string
if stackConfig != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/dashboard/server/releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ func release(w http.ResponseWriter, r *http.Request) {
Image: imageRepository,
Tag: imageTag,
Dockerfile: dockerfile,
Strategy: strategy,
Registry: registry,
}
break
Expand Down
40 changes: 2 additions & 38 deletions pkg/dashboard/worker/gitops.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ import (
"github.com/gimlet-io/gimlet/pkg/dx"
"github.com/gimlet-io/gimlet/pkg/git/customScm"
"github.com/gimlet-io/gimlet/pkg/git/nativeGit"
helper "github.com/gimlet-io/gimlet/pkg/git/nativeGit"
bootstrap "github.com/gimlet-io/gimlet/pkg/gitops"
"github.com/gimlet-io/gimlet/pkg/gitops/sync"
"github.com/joho/godotenv"
"sigs.k8s.io/yaml"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
Expand Down Expand Up @@ -726,23 +724,14 @@ func cloneTemplateWriteAndPush(
}
}

var stackConfig *dx.StackConfig
stackYamlPath := "stack.yaml"
if !envFromStore.RepoPerEnv {
stackYamlPath = filepath.Join(envFromStore.Name, "stack.yaml")
}

err = gitopsRepoCache.PerformAction(envFromStore.InfraRepo, func(repo *git.Repository) error {
var inerErr error
stackConfig, inerErr = stackYaml(repo, stackYamlPath)
return inerErr
})
stackConfig, err := server.StackConfig(gitopsRepoCache, stackYamlPath, envFromStore.InfraRepo)
if err != nil {
if !strings.Contains(err.Error(), "file not found") {
return "", err
} else {
return "", err
}
return "", err
}

imagepullSecretManifest, err := imagepullSecretTemplate(
Expand Down Expand Up @@ -808,28 +797,6 @@ func cloneTemplateWriteAndPush(
return sha, nil
}

// TODO Duplication
func stackYaml(repo *git.Repository, path string) (*dx.StackConfig, error) {
var stackConfig dx.StackConfig

headBranch, err := helper.HeadBranch(repo)
if err != nil {
return nil, err
}

yamlString, err := helper.RemoteContentOnBranchWithoutCheckout(repo, headBranch, path)
if err != nil {
return nil, err
}

err = yaml.Unmarshal([]byte(yamlString), &stackConfig)
if err != nil {
return nil, err
}

return &stackConfig, nil
}

func cloneTemplateDeleteAndPush(
gitopsRepoCache *nativeGit.RepoCache,
cleanupPolicy *dx.Cleanup,
Expand Down Expand Up @@ -1228,7 +1195,6 @@ func kustomizationTemplate(
repoPerEnv)
}

// TODO CLEANUP IN CASE OF APP DELETE!!!
func imagepullSecretTemplate(
manifest *dx.Manifest,
stackConfig *dx.StackConfig,
Expand Down Expand Up @@ -1261,11 +1227,9 @@ func imagepullSecretTemplate(
encryptedConfigString = encryptedConfig.(string)
}

secretName := fmt.Sprintf("%s-pullsecret", strings.ToLower(registryString))
return sync.GenerateImagePullSecret(
manifest.Env,
manifest.App,
secretName,
manifest.Namespace,
encryptedConfigString,
repoPerEnv,
Expand Down
10 changes: 5 additions & 5 deletions pkg/dashboard/worker/gitops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func Test_gitopsTemplateAndWrite(t *testing.T) {
repo.CreateRemote(&config.RemoteConfig{Name: "origin", URLs: []string{""}})

repoPerEnv := false
_, err := gitopsTemplateAndWrite(repo, a.Environments[0], &dx.Release{}, "", repoPerEnv, nil, nil, nil)
_, err := gitopsTemplateAndWrite(repo, a.Environments[0], &dx.Release{}, "", repoPerEnv, nil, nil, nil, nil)
assert.Nil(t, err)
content, _ := nativeGit.Content(repo, "staging/my-app/deployment.yaml")
assert.True(t, len(content) > 100)
Expand All @@ -107,7 +107,7 @@ func Test_gitopsTemplateAndWrite(t *testing.T) {
assert.True(t, len(content) > 1)

repoPerEnv = true
_, err = gitopsTemplateAndWrite(repo, a.Environments[0], &dx.Release{}, "", repoPerEnv, nil, nil, nil)
_, err = gitopsTemplateAndWrite(repo, a.Environments[0], &dx.Release{}, "", repoPerEnv, nil, nil, nil, nil)
assert.Nil(t, err)
content, _ = nativeGit.Content(repo, "my-app/deployment.yaml")
assert.True(t, len(content) > 100)
Expand Down Expand Up @@ -163,10 +163,10 @@ func Test_gitopsTemplateAndWrite_deleteStaleFiles(t *testing.T) {
json.Unmarshal([]byte(withVolume), &a)

repoPerEnv := true
_, err := gitopsTemplateAndWrite(repo, a.Environments[0], &dx.Release{}, "", repoPerEnv, nil, nil, nil)
_, err := gitopsTemplateAndWrite(repo, a.Environments[0], &dx.Release{}, "", repoPerEnv, nil, nil, nil, nil)
assert.Nil(t, err)

_, err = gitopsTemplateAndWrite(repo, a.Environments[0], &dx.Release{}, "", repoPerEnv, nil, nil, nil)
_, err = gitopsTemplateAndWrite(repo, a.Environments[0], &dx.Release{}, "", repoPerEnv, nil, nil, nil, nil)
assert.Nil(t, err)

content, _ := nativeGit.Content(repo, "my-app/deployment.yaml")
Expand Down Expand Up @@ -204,7 +204,7 @@ func Test_gitopsTemplateAndWrite_deleteStaleFiles(t *testing.T) {

var b dx.Artifact
json.Unmarshal([]byte(withoutVolume), &b)
_, err = gitopsTemplateAndWrite(repo, b.Environments[0], &dx.Release{}, "", false, nil, nil, nil)
_, err = gitopsTemplateAndWrite(repo, b.Environments[0], &dx.Release{}, "", false, nil, nil, nil, nil)
assert.Nil(t, err)

content, _ = nativeGit.Content(repo, "staging/my-app/pvc.yaml")
Expand Down
5 changes: 2 additions & 3 deletions pkg/gitops/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,7 @@ func GenerateKustomizationForApp(
}

func GenerateImagePullSecret(
env, app string,
secretName, namespace string,
env, app, namespace string,
encryptedDockerconfigjson string,
singleEnv bool,
) (*manifestgen.Manifest, error) {
Expand All @@ -280,7 +279,7 @@ func GenerateImagePullSecret(
APIVersion: "bitnami.com/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
Name: "regcred",
Namespace: namespace,
Annotations: map[string]string{
"sealedsecrets.bitnami.com/cluster-wide": "true",
Expand Down
42 changes: 37 additions & 5 deletions web/dashboard/src/views/envConfig/envConfig.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class EnvConfig extends Component {

this.setValues = this.setValues.bind(this);
this.resetNotificationStateAfterThreeSeconds = this.resetNotificationStateAfterThreeSeconds.bind(this);
this.setImagePullSecret = this.setImagePullSecret.bind(this);
}

componentDidMount() {
Expand All @@ -59,6 +60,14 @@ class EnvConfig extends Component {

const { gimletClient } = this.props;

gimletClient.getStackConfig(env)
.then(data => {
this.setState({
stackConfig: data.stackConfig,
stackDefinition: data.stackDefinition
});
}, () => {/* Generic error handler deals with it */ });

if (action === "new") {
gimletClient.getDefaultDeploymentTemplates()
.then(data => {
Expand Down Expand Up @@ -455,6 +464,18 @@ class EnvConfig extends Component {
})
}

setImagePullSecret(secret) {
this.setState(prevState => ({
configFile: {
...prevState.configFile,
values: {
...prevState.configFile.values,
imagePullSecrets: [secret]
}
}
}));
}

renderTemplateFromConfig() {
let title = "Web application template"
let description = "To deploy any web application. Multiple image build options available."
Expand Down Expand Up @@ -530,11 +551,6 @@ class EnvConfig extends Component {

const hasChange = JSON.stringify(this.state.configFile) !== JSON.stringify(this.state.defaultConfigFile)

const customFields = {
imageWidget: ImageWidget,
sealedSecretWidget: (props) => <SealedSecretWidget {...props} gimletClient={this.props.gimletClient} store={this.props.store} env={env} />,
}

if (!this.state.configFile) {
return <Spinner />;
}
Expand All @@ -543,6 +559,15 @@ class EnvConfig extends Component {
return <Spinner />;
}

if (!this.state.stackConfig) {
return <Spinner />;
}

const customFields = {
imageWidget: (props) => <ImageWidget {...props} setImagePullSecret={this.setImagePullSecret} registry={filterRegistry(this.state.stackConfig, this.state.stackDefinition)} />,
sealedSecretWidget: (props) => <SealedSecretWidget {...props} gimletClient={this.props.gimletClient} store={this.props.store} env={env} />,
}

return (
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<h1 className="text-3xl font-bold leading-tight text-gray-900">Editing {config} config for {env}</h1>
Expand Down Expand Up @@ -850,4 +875,11 @@ class EnvConfig extends Component {
}
}

const filterRegistry = (stackConfig, stackDefinition) => {
const config = stackConfig.config;
const elements = stackDefinition.components.filter(c => c.category === "registry")

return Object.fromEntries(Object.entries(config).filter(([key]) => elements.some(e => e.variable === key)))
}

export default EnvConfig;
54 changes: 33 additions & 21 deletions web/dashboard/src/views/envConfig/imageWidget.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ class ImageWidget extends Component {
};
}

// TODO bug input field loses focus on writing
componentDidMount() {
this.props.setImagePullSecret("regcred")
}

defaults(strategy) {
let repository = ""
let tag = ""
Expand Down Expand Up @@ -54,37 +59,41 @@ 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, "registry": this.state.registry })
() => 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 }}"
}
selectOnChange(name) {
return (event) => {
const { registry } = this.props;
const login = registry[event.target.value].login ?? "your-company"
let repository = "";

switch (event.target.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 });
this.setState(
{
[name]: event.target.value,
},
() => this.props.onChange({"repository": repository, "tag": this.state.tag, "dockerfile": this.state.dockerfile, "strategy": this.state.strategy, "registry": this.state.registry })
);
};
}

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 Down Expand Up @@ -168,9 +177,12 @@ class ImageWidget extends Component {
</div>
}
{(strategy === "dockerfile" || strategy === "buildpacks") &&
Object.keys(registry).length !== 0 &&
<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)}>
<select id="root_1__anyof_select" className="form-control" value={this.state.registry}
onChange={this.selectOnChange('registry')}
>
{
Object.keys(registry).map((item, idx) => {
if (!registry[item].hasOwnProperty("enabled")) {
Expand Down

0 comments on commit 6b8a592

Please sign in to comment.