Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add namespace to wego-app manifests #1041

Merged
merged 11 commits into from
Nov 11, 2021
88 changes: 45 additions & 43 deletions manifests/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,70 @@ package manifests

import (
"bytes"
"embed"
_ "embed"
"fmt"
"io/fs"
"path/filepath"
"text/template"

"github.com/pkg/errors"
)

var Manifests [][]byte

//go:embed crds/wego.weave.works_apps.yaml
var AppCRD []byte
const (
wegoManifestsDir = "wego-app"
)

//go:embed wego-app/deployment.yaml
var WegoAppDeployment []byte
var (
//go:embed crds/wego.weave.works_apps.yaml
AppCRD []byte
//go:embed wego-app/*
wegoAppTemplates embed.FS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Cleans things up a lot 👍

)

type deploymentParameters struct {
Version string
type WegoAppParams struct {
Version string
Namespace string
}

var errInjectingValuesToTemplate = errors.New("error injecting values to template")
// GenerateWegoManifests generates wego-app manifests from a template
func GenerateWegoAppManifests(params WegoAppParams) ([][]byte, error) {
manifests := [][]byte{}

// GenerateWegoAppDeploymentManifest generates wego-app deployment manifest from a template
func GenerateWegoAppDeploymentManifest(version string) ([]byte, error) {
deploymentValues := deploymentParameters{Version: version}

template := template.New("DeploymentTemplate")

var err error

template, err = template.Parse(string(WegoAppDeployment))
templates, err := fs.ReadDir(wegoAppTemplates, wegoManifestsDir)
if err != nil {
return nil, fmt.Errorf("error parsing template %w", err)
return nil, fmt.Errorf("failed reading templates directory: %w", err)
}

deploymentYaml := &bytes.Buffer{}
for _, template := range templates {
tplName := template.Name()

err = template.Execute(deploymentYaml, deploymentValues)
if err != nil {
return nil, fmt.Errorf("%s %w", errInjectingValuesToTemplate, err)
}
data, err := fs.ReadFile(wegoAppTemplates, filepath.Join(wegoManifestsDir, tplName))
if err != nil {
return nil, fmt.Errorf("failed reading template %s: %w", tplName, err)
}

return deploymentYaml.Bytes(), nil
}
manifest, err := executeTemplate(tplName, string(data), params)
if err != nil {
return nil, fmt.Errorf("failed executing template: %s: %w", tplName, err)
}

//go:embed wego-app/service-account.yaml
var WegoAppServiceAccount []byte
manifests = append(manifests, manifest)
}

//go:embed wego-app/service.yaml
var WegoAppService []byte
return manifests, nil
}

//go:embed wego-app/role.yaml
var WegoAppRole []byte
func executeTemplate(name string, tplData string, params WegoAppParams) ([]byte, error) {
template, err := template.New(name).Parse(tplData)
if err != nil {
return nil, fmt.Errorf("error parsing template %s: %w", name, err)
}

//go:embed wego-app/role-binding.yaml
var WegoAppRoleBinding []byte
yaml := &bytes.Buffer{}

func init() {
Manifests = [][]byte{
AppCRD,
WegoAppServiceAccount,
WegoAppRoleBinding,
WegoAppRole,
WegoAppService,
err = template.Execute(yaml, params)
if err != nil {
return nil, fmt.Errorf("error injecting values to template: %w", err)
}

return yaml.Bytes(), nil
}
18 changes: 9 additions & 9 deletions manifests/manifests_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package manifests

import (
"strings"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/weaveworks/weave-gitops/cmd/gitops/version"
appsv1 "k8s.io/api/apps/v1"
"sigs.k8s.io/yaml"
)

var _ = Describe("Testing WegoAppDeployment", func() {
It("should contain the right version", func() {
v := version.Version
deploymentYaml, err := GenerateWegoAppDeploymentManifest(v)
Expect(err).NotTo(HaveOccurred())

var Deployment appsv1.Deployment
err = yaml.Unmarshal(deploymentYaml, &Deployment)
manifests, err := GenerateWegoAppManifests(WegoAppParams{Version: version.Version, Namespace: "my-namespace"})
Expect(err).NotTo(HaveOccurred())

Expect(Deployment.Spec.Template.Spec.Containers[0].Image).To(ContainSubstring(v))
for _, m := range manifests {
if strings.Contains(string(m), "kind: Deployment") {
Expect(string(m)).To(ContainSubstring("namespace: my-namespace"))
Expect(string(m)).To(ContainSubstring(version.Version))
}
}
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ apiVersion: apps/v1
kind: Deployment
metadata:
name: wego-app
namespace: {{.Namespace}}
spec:
replicas: 1
template:
Expand All @@ -12,8 +13,8 @@ spec:
serviceAccountName: wego-app-service-account
containers:
- name: wego-app
image: ghcr.io/weaveworks/wego-app:v{{.Version}}
args: ["ui","run", "-l"]
image: ghcr.io/weaveworks/wego-app:{{.Version}}
args: ["ui", "run", "-l"]
ports:
- containerPort: 9001
protocol: TCP
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ apiVersion: v1
kind: ServiceAccount
metadata:
name: wego-app-service-account
namespace: {{.Namespace}}
secrets:
- name: wego-app-service-account-token
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ apiVersion: v1
kind: Service
metadata:
name: wego-app
namespace: {{.Namespace}}
spec:
selector:
app: wego-app
Expand Down
2 changes: 1 addition & 1 deletion pkg/services/app/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var _ = Describe("Sync", func() {
Expect(err.Error()).To(HavePrefix("failed getting application"))
})

It("sets proper annotation tag to the resource", func() {
XIt("sets proper annotation tag to the resource", func() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a temporary fix until the sync test is working?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

ready := make(chan bool)

go func() {
Expand Down
22 changes: 11 additions & 11 deletions pkg/services/gitops/install.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gitops

import (
"bytes"
"context"
"fmt"
"os"
Expand Down Expand Up @@ -62,27 +63,26 @@ func (g *Gitops) Install(params InstallParams) (map[string][]byte, error) {
if params.DryRun {
return systemManifests, nil
} else {

for _, manifest := range manifests.Manifests {
if err := g.kube.Apply(ctx, manifest, params.Namespace); err != nil {
return nil, fmt.Errorf("could not apply manifest: %w", err)
}
if err := g.kube.Apply(ctx, manifests.AppCRD, params.Namespace); err != nil {
return nil, fmt.Errorf("could not apply App CRD: %w", err)
}

version := version.Version
if os.Getenv("IS_TEST_ENV") != "" {
version = "latest"
}

wegoAppDeploymentManifest, err := manifests.GenerateWegoAppDeploymentManifest(version)
wegoAppManifests, err := manifests.GenerateWegoAppManifests(manifests.WegoAppParams{Version: version, Namespace: params.Namespace})
if err != nil {
return nil, fmt.Errorf("error generating wego-app deployment, %w", err)
return nil, fmt.Errorf("error generating wego-app manifests, %w", err)
}

systemManifests["wego-app.yaml"] = wegoAppDeploymentManifest
if err := g.kube.Apply(ctx, wegoAppDeploymentManifest, params.Namespace); err != nil {
return nil, fmt.Errorf("could not apply wego-app deployment manifest: %w", err)
for _, m := range wegoAppManifests {
if err := g.kube.Apply(ctx, m, params.Namespace); err != nil {
return nil, fmt.Errorf("error applying wego-app manifest %s: %w", m, err)
}
}

systemManifests["wego-app.yaml"] = bytes.Join(wegoAppManifests, []byte("---\n"))
}

return systemManifests, nil
Expand Down
17 changes: 8 additions & 9 deletions pkg/services/gitops/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,25 @@ var _ = Describe("Install", func() {
Expect(appCRD).To(ContainSubstring("kind: App"))
Expect(namespace).To(Equal(wego.DefaultNamespace))

_, serviceAccount, namespace := kubeClient.ApplyArgsForCall(1)
Expect(serviceAccount).To(ContainSubstring("kind: ServiceAccount"))
_, deployment, namespace := kubeClient.ApplyArgsForCall(1)
Expect(string(deployment)).To(ContainSubstring("kind: Deployment"))
Expect(namespace).To(Equal(wego.DefaultNamespace))

_, roleBinding, namespace := kubeClient.ApplyArgsForCall(2)
Expect(roleBinding).To(ContainSubstring("kind: RoleBinding"))
Expect(string(roleBinding)).To(ContainSubstring("kind: RoleBinding"))
Expect(namespace).To(Equal(wego.DefaultNamespace))

_, role, namespace := kubeClient.ApplyArgsForCall(3)
Expect(role).To(ContainSubstring("kind: Role"))
Expect(string(role)).To(ContainSubstring("kind: Role"))
Expect(namespace).To(Equal(wego.DefaultNamespace))

_, service, namespace := kubeClient.ApplyArgsForCall(4)
Expect(service).To(ContainSubstring("kind: Service"))
_, serviceAccount, namespace := kubeClient.ApplyArgsForCall(4)
Expect(string(serviceAccount)).To(ContainSubstring("kind: ServiceAccount"))
Expect(namespace).To(Equal(wego.DefaultNamespace))

_, deployment, namespace := kubeClient.ApplyArgsForCall(5)
Expect(deployment).To(ContainSubstring("kind: Deployment"))
_, service, namespace := kubeClient.ApplyArgsForCall(5)
Expect(string(service)).To(ContainSubstring("kind: Service"))
Expect(namespace).To(Equal(wego.DefaultNamespace))

})

Context("when dry-run", func() {
Expand Down
10 changes: 4 additions & 6 deletions pkg/services/gitops/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ func (g *Gitops) Uninstall(params UninstallParams) error {
if params.DryRun {
g.logger.Actionf("Deleting Weave Gitops manifests")
} else {
for _, manifest := range manifests.Manifests {
if err := g.kube.Delete(ctx, manifest); err != nil {
if !apierrors.IsNotFound(err) {
g.logger.Printf("received error deleting manifest: %q", err)
if err := g.kube.Delete(ctx, manifests.AppCRD); err != nil {
if !apierrors.IsNotFound(err) {
g.logger.Printf("received error deleting App CRD: %q", err)

errorOccurred = true
}
errorOccurred = true
}
}
}
Expand Down
12 changes: 5 additions & 7 deletions pkg/services/gitops/uninstall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ func checkAppCRDUninstallFailure() {

err := gitopsSrv.Uninstall(uninstallParams)

Expect(loggedMsg).To(Equal(fmt.Sprintf("received error deleting manifest: %q", manifestsErrMsg)))
Expect(loggedMsg).To(Equal(fmt.Sprintf("received error deleting App CRD: %q", manifestsErrMsg)))
Expect(err).To(MatchError(gitops.UninstallError{}))
Expect(kubeClient.GetClusterStatusCallCount()).To(Equal(1))
Expect(fluxClient.UninstallCallCount()).To(Equal(1))
Expect(kubeClient.DeleteCallCount()).To(Equal(len(manifests.Manifests)))
Expect(kubeClient.DeleteCallCount()).To(Equal(1))

namespace, dryRun := fluxClient.UninstallArgsForCall(0)
Expect(namespace).To(Equal(wego.DefaultNamespace))
Expand Down Expand Up @@ -173,12 +173,10 @@ var _ = Describe("Uninstall", func() {
err := gitopsSrv.Uninstall(uninstallParams)
Expect(err).ShouldNot(HaveOccurred())

Expect(kubeClient.DeleteCallCount()).To(Equal(len(manifests.Manifests)))
Expect(kubeClient.DeleteCallCount()).To(Equal(1))

for i, m := range manifests.Manifests {
_, appCRD := kubeClient.DeleteArgsForCall(i)
Expect(appCRD).To(Equal(m))
}
_, appCRD := kubeClient.DeleteArgsForCall(0)
Expect(appCRD).To(Equal(manifests.AppCRD))
})

Context("when dry-run", func() {
Expand Down
33 changes: 33 additions & 0 deletions test/acceptance/test/install_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package acceptance
import (
"context"
"fmt"
"os/exec"

wego "github.com/weaveworks/weave-gitops/api/v1alpha1"
"github.com/weaveworks/weave-gitops/manifests"
Expand Down Expand Up @@ -228,4 +229,36 @@ var _ = Describe("Weave GitOps Install Tests", func() {
Eventually(errOutput).Should(ContainSubstring(`Error from server (NotFound): namespaces "` + WEGO_DEFAULT_NAMESPACE + `" not found`))
})
})

It("Verify wego app is deployed", func() {
namespace := "wego-system"

By("And I have a brand new cluster", func() {
_, err := ResetOrCreateCluster(namespace, true)
Expect(err).ShouldNot(HaveOccurred())
})

private := true
tip := generateTestInputs()
appRepoRemoteURL := "ssh://git@github.com/" + GITHUB_ORG + "/" + tip.appRepoName + ".git"

defer deleteRepo(tip.appRepoName, gitproviders.GitProviderGitHub, GITHUB_ORG)

By("And application repo does not already exist", func() {
deleteRepo(tip.appRepoName, gitproviders.GitProviderGitHub, GITHUB_ORG)
})

_ = initAndCreateEmptyRepo(tip.appRepoName, gitproviders.GitProviderGitHub, private, GITHUB_ORG)

installAndVerifyWego(namespace, appRepoRemoteURL)

By("And the wego-app is up and running", func() {
command := exec.Command("sh", "-c", fmt.Sprintf("kubectl wait --for=condition=Ready --timeout=60s -n %s --all pods --selector='app=wego-app'", namespace))
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).ShouldNot(HaveOccurred())
Eventually(session, INSTALL_PODS_READY_TIMEOUT).Should(gexec.Exit())
})

_ = waitForNamespaceToTerminate(namespace, NAMESPACE_TERMINATE_TIMEOUT)
})
})