Skip to content

Commit

Permalink
Set vcs-uri annotation on podman (#6746)
Browse files Browse the repository at this point in the history
* Set vcs-uri annotation on podman

* Integration test

* Disable test with Podman v3

* Make test run with podman v4

* Fix displaying error
  • Loading branch information
feloy authored Apr 19, 2023
1 parent 3aeaeb5 commit b843ce8
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 21 deletions.
28 changes: 21 additions & 7 deletions pkg/dev/podmandev/pod.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package podmandev

import (
"context"
"fmt"
"math/rand" // #nosec
"sort"
Expand All @@ -17,6 +18,7 @@ import (
"github.com/redhat-developer/odo/pkg/labels"
"github.com/redhat-developer/odo/pkg/libdevfile"
"github.com/redhat-developer/odo/pkg/odo/commonflags"
odocontext "github.com/redhat-developer/odo/pkg/odo/context"
"github.com/redhat-developer/odo/pkg/storage"
"github.com/redhat-developer/odo/pkg/util"

Expand All @@ -31,9 +33,7 @@ const (
)

func createPodFromComponent(
devfileObj parser.DevfileObj,
componentName string,
appName string,
ctx context.Context,
debug bool,
buildCommand string,
runCommand string,
Expand All @@ -43,7 +43,14 @@ func createPodFromComponent(
customForwardedPorts []api.ForwardedPort,
usedPorts []int,
) (*corev1.Pod, []api.ForwardedPort, error) {
podTemplate, err := generator.GetPodTemplateSpec(devfileObj, generator.PodTemplateParams{})
var (
appName = odocontext.GetApplication(ctx)
componentName = odocontext.GetComponentName(ctx)
devfileObj = odocontext.GetDevfileObj(ctx)
workingDir = odocontext.GetWorkingDirectory(ctx)
)

podTemplate, err := generator.GetPodTemplateSpec(*devfileObj, generator.PodTemplateParams{})
if err != nil {
return nil, nil, err
}
Expand All @@ -53,7 +60,7 @@ func createPodFromComponent(
}

var fwPorts []api.ForwardedPort
fwPorts, err = getPortMapping(devfileObj, debug, randomPorts, usedPorts, customForwardedPorts)
fwPorts, err = getPortMapping(*devfileObj, debug, randomPorts, usedPorts, customForwardedPorts)
if err != nil {
return nil, nil, err
}
Expand All @@ -80,7 +87,7 @@ func createPodFromComponent(
},
}

devfileVolumes, err := storage.ListStorage(devfileObj)
devfileVolumes, err := storage.ListStorage(*devfileObj)
if err != nil {
return nil, nil, err
}
Expand All @@ -100,7 +107,7 @@ func createPodFromComponent(
}
}

containers, err = utils.UpdateContainersEntrypointsIfNeeded(devfileObj, containers, buildCommand, runCommand, debugCommand)
containers, err = utils.UpdateContainersEntrypointsIfNeeded(*devfileObj, containers, buildCommand, runCommand, debugCommand)
if err != nil {
return nil, nil, err
}
Expand All @@ -125,6 +132,13 @@ func createPodFromComponent(
pod.SetLabels(labels.GetLabels(componentName, appName, runtime, labels.ComponentDevMode, true))
labels.SetProjectType(pod.GetLabels(), component.GetComponentTypeFromDevfileMetadata(devfileObj.Data.GetMetadata()))

if pod.Annotations == nil {
pod.Annotations = make(map[string]string)
}
if vcsUri := util.GetGitOriginPath(workingDir); vcsUri != "" {
pod.Annotations["app.openshift.io/vcs-uri"] = vcsUri
}

return &pod, fwPorts, nil
}

Expand Down
12 changes: 9 additions & 3 deletions pkg/dev/podmandev/pod_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package podmandev

import (
"context"
"testing"

"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
Expand All @@ -12,6 +13,7 @@ import (
"github.com/redhat-developer/odo/pkg/api"
"github.com/redhat-developer/odo/pkg/labels"
"github.com/redhat-developer/odo/pkg/libdevfile/generator"
odocontext "github.com/redhat-developer/odo/pkg/odo/context"
"github.com/redhat-developer/odo/pkg/version"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -1290,10 +1292,14 @@ func Test_createPodFromComponent(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
devfileObj := tt.args.devfileObj()
ctx = odocontext.WithDevfileObj(ctx, &devfileObj)
ctx = odocontext.WithApplication(ctx, tt.args.appName)
ctx = odocontext.WithComponentName(ctx, tt.args.componentName)
ctx = odocontext.WithWorkingDirectory(ctx, "/tmp/dir")
got, gotFwPorts, err := createPodFromComponent(
tt.args.devfileObj(),
tt.args.componentName,
tt.args.appName,
ctx,
tt.args.debug,
tt.args.buildCommand,
tt.args.runCommand,
Expand Down
9 changes: 1 addition & 8 deletions pkg/dev/podmandev/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,19 +196,12 @@ func (o *DevClient) buildPushAutoImageComponents(ctx context.Context, devfileObj

// deployPod deploys the component as a Pod in podman
func (o *DevClient) deployPod(ctx context.Context, options dev.StartOptions) (*corev1.Pod, []api.ForwardedPort, error) {
var (
appName = odocontext.GetApplication(ctx)
componentName = odocontext.GetComponentName(ctx)
devfileObj = odocontext.GetDevfileObj(ctx)
)

spinner := log.Spinner("Deploying pod")
defer spinner.End(false)

pod, fwPorts, err := createPodFromComponent(
*devfileObj,
componentName,
appName,
ctx,
options.Debug,
options.BuildCommand,
options.RunCommand,
Expand Down
11 changes: 11 additions & 0 deletions tests/helper/component_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ func (o *ClusterComponent) GetLabels() map[string]string {
return result
}

func (o *ClusterComponent) GetAnnotations() map[string]string {
selector := labels.Builder().WithComponentName(o.name).WithAppName(o.app).WithMode(o.mode).SelectorFlag()
stdout := o.cli.Run("get", "deployment", selector, "-n", o.namespace, "-o", "jsonpath={.items[0].metadata.annotations}").Out.Contents()

var result map[string]string
err := json.Unmarshal(stdout, &result)
Expect(err).ToNot(HaveOccurred())

return result
}

func (o *ClusterComponent) GetPodDef() *corev1.Pod {
var podDef corev1.Pod
podName := o.cli.GetRunningPodNameByComponent(o.name, o.namespace)
Expand Down
2 changes: 2 additions & 0 deletions tests/helper/component_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type Component interface {
GetEnvVars(container string) map[string]string
// GetLabels returns the labels defined for the component
GetLabels() map[string]string
// GetAnnotations returns the annotations defined for the component
GetAnnotations() map[string]string
// GetPodDef returns the definition of the pod
GetPodDef() *corev1.Pod
// GetJobDef returns the definition of the job
Expand Down
5 changes: 5 additions & 0 deletions tests/helper/component_podman.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ func (o *PodmanComponent) GetLabels() map[string]string {
return result.Labels
}

func (o *PodmanComponent) GetAnnotations() map[string]string {
def := o.GetPodDef()
return def.Annotations
}

func (o *PodmanComponent) GetPodLogs() string {
podName := fmt.Sprintf("%s-%s", o.componentName, o.app)
cmd := exec.Command("podman", "pod", "logs", podName)
Expand Down
19 changes: 19 additions & 0 deletions tests/helper/helper_podman.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package helper

import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"

. "github.com/onsi/gomega"
"github.com/redhat-developer/odo/pkg/podman"
)

func GenerateAndSetContainersConf(dir string) {
Expand Down Expand Up @@ -42,3 +45,19 @@ func ExtractK8sAndOcComponentsFromOutputOnPodman(out string) []string {

return handled
}

// Returns version of installed podman
func GetPodmanVersion() string {
cmd := exec.Command("podman", "version", "--format", "json")
out, err := cmd.Output()
Expect(err).ToNot(HaveOccurred(), func() string {
if exiterr, ok := err.(*exec.ExitError); ok {
err = fmt.Errorf("%s: %s", err, string(exiterr.Stderr))
}
return err.Error()
})
var result podman.SystemVersionReport
err = json.Unmarshal(out, &result)
Expect(err).ToNot(HaveOccurred())
return result.Client.Version
}
11 changes: 8 additions & 3 deletions tests/integration/cmd_dev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2375,7 +2375,10 @@ CMD ["npm", "start"]
devfileCmpName := "nodejs"
BeforeEach(func() {
if podman {
Skip("Not implemented yet on Podman - see https://github.com/redhat-developer/odo/issues/6493")
version := helper.GetPodmanVersion()
if strings.HasPrefix(version, "3.") {
Skip("Getting annotations is not available with Podman v3")
}
}
helper.Cmd("git", "init").ShouldPass()
remote := "origin"
Expand All @@ -2389,10 +2392,12 @@ CMD ["npm", "start"]
err := helper.RunDevMode(helper.DevSessionOpts{
RunOnPodman: podman,
}, func(session *gexec.Session, outContents []byte, errContents []byte, ports map[string]string) {
annotations := commonVar.CliRunner.GetAnnotationsDeployment(devfileCmpName, "app", commonVar.Project)
component := helper.NewComponent(devfileCmpName, "app", labels.ComponentDevMode, commonVar.Project, commonVar.CliRunner)
annotations := component.GetAnnotations()
var valueFound bool
for key, value := range annotations {
if key == "app.openshift.io/vcs-uri" && value == remoteURL {
// Pdoman adds a suffix to the annotation key with the name of the container
if strings.HasPrefix(key, "app.openshift.io/vcs-uri") && value == remoteURL {
valueFound = true
break
}
Expand Down

0 comments on commit b843ce8

Please sign in to comment.