Skip to content

Commit

Permalink
PAC: Update info configmap with controller route
Browse files Browse the repository at this point in the history
Signed-off-by: Shivam Mukhade <smukhade@redhat.com>

Signed-off-by: Shivam Mukhade <smukhade@redhat.com>
  • Loading branch information
Shivam Mukhade committed Jun 24, 2022
1 parent 755d272 commit 80850ed
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
4 changes: 2 additions & 2 deletions hack/fetch-releases.sh
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ release_yaml_pac() {
do
echo "fetching PipelineRun template for runtime: $run"

source="https://raw.githubusercontent.com/openshift-pipelines/pipelines-as-code/main/pkg/cmd/tknpac/generate/templates/${run}-template.yaml"
source="https://raw.githubusercontent.com/openshift-pipelines/pipelines-as-code/${version}/pkg/cmd/tknpac/generate/templates/${run}.yaml"
dest_dir="${ko_data}/tekton-addon/pipelines-as-code-templates"
mkdir -p ${dest_dir} || true
destination="${dest_dir}/${run}-template.yaml"
destination="${dest_dir}/${run}.yaml"

http_response=$(curl -s -o ${destination} -w "%{http_code}" ${source})
echo url: ${source}
Expand Down
89 changes: 87 additions & 2 deletions pkg/reconciler/openshift/tektonaddon/pipelinesascode.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"path/filepath"
"strings"

routev1 "github.com/openshift/api/route/v1"
"github.com/openshift/client-go/route/clientset/versioned/scheme"
"github.com/tektoncd/operator/pkg/reconciler/openshift"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -59,22 +61,51 @@ func (r *Reconciler) EnsurePipelinesAsCode(ctx context.Context, ta *v1alpha1.Tek
}

if *ta.Spec.EnablePAC {

exist, err := checkIfInstallerSetExist(ctx, r.operatorClientSet, r.operatorVersion, pacLabelSelector)
if err != nil {
return err
}
if !exist {
return r.ensurePAC(ctx, ta)
}
return r.updateControllerURL(ta)

} else {
// if disabled then delete the installer Set if exist
if err := r.deleteInstallerSet(ctx, pacLabelSelector); err != nil {
return err
}
}
return nil
}

func (r *Reconciler) updateControllerURL(ta *v1alpha1.TektonAddon) error {
var err error
pacManifest := mf.Manifest{
Client: r.manifest.Client,
}

koDataDir := os.Getenv(common.KoEnvKey)
pacLocation := filepath.Join(koDataDir, "tekton-addon", "pipelines-as-code")
if err := common.AppendManifest(&pacManifest, pacLocation); err != nil {
return err
}
pacManifest, err = pacManifest.Transform(mf.InjectNamespace(ta.Spec.TargetNamespace))
if err != nil {
return err
}

route, err := getControllerRouteHost(&pacManifest)
if err != nil {
return err
}
if route == "" {
return v1alpha1.RECONCILE_AGAIN_ERR
}

if err := updateInfoConfigMap(route, &pacManifest); err != nil {
return err
}
return nil
}

Expand Down Expand Up @@ -158,7 +189,7 @@ func pipelineRunToConfigMapConverter(prManifests *mf.Manifest) (*mf.Manifest, er
// set metadata
prname := res.GetName()
cm.SetName("pipelines-as-code-" + prname)
cm.Labels[pacRuntimeLabel] = strings.TrimSuffix(prname, "-template")
cm.Labels[pacRuntimeLabel] = strings.TrimPrefix(prname, "pipelinerun-")

unstrObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(cm)
if err != nil {
Expand All @@ -170,3 +201,57 @@ func pipelineRunToConfigMapConverter(prManifests *mf.Manifest) (*mf.Manifest, er
manifest, _ := mf.ManifestFrom(mf.Slice(temp))
return &manifest, nil
}

func getControllerRouteHost(manifest *mf.Manifest) (string, error) {
var hostUrl string
for _, r := range manifest.Filter(mf.ByKind("Route")).Resources() {
u, err := manifest.Client.Get(&r)
if err != nil {
return "", err
}
if u.GetName() == "pipelines-as-code-controller" {
route := &routev1.Route{}
if err := scheme.Scheme.Convert(u, route, nil); err != nil {
return "", err
}
hostUrl = route.Spec.Host
}
}
return hostUrl, nil
}

func updateInfoConfigMap(route string, pacManifest *mf.Manifest) error {
for _, r := range pacManifest.Filter(mf.ByKind("ConfigMap")).Resources() {
if r.GetName() != "pipelines-as-code-info" {
continue
}
u, err := pacManifest.Client.Get(&r)
if err != nil {
return err
}
cm := &v1.ConfigMap{}
err = runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, cm)
if err != nil {
return err
}

// set controller url
if cm.Data["controller-url"] != "" {
return nil
}

cm.Data["controller-url"] = "https://" + route

unstrObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(cm)
if err != nil {
return err
}
u.SetUnstructuredContent(unstrObj)

err = pacManifest.Client.Update(u)
if err != nil {
return err
}
}
return nil
}

0 comments on commit 80850ed

Please sign in to comment.