-
Notifications
You must be signed in to change notification settings - Fork 248
/
argocd_controller.go
203 lines (173 loc) · 6.95 KB
/
argocd_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
Copyright 2021.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controllers
import (
"context"
"encoding/base64"
"fmt"
"os"
"strings"
// embed the Argo icon during compile time
_ "embed"
"github.com/go-logr/logr"
console "github.com/openshift/api/console/v1"
routev1 "github.com/openshift/api/route/v1"
"github.com/redhat-developer/gitops-operator/common"
"github.com/redhat-developer/gitops-operator/controllers/util"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
const (
argocdNS = "openshift-gitops"
depracatedArgoCDNS = "openshift-pipelines-app-delivery"
consoleLinkName = "argocd"
argocdRouteName = "openshift-gitops-server"
iconFilePath = "/argo.png"
operatorPodNamespacePath = "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
)
var (
encodedArgoImage string
//go:embed argocd/img/argo.png
argoImage []byte
)
func init() {
encodedArgoImage = imageDataURL(base64.StdEncoding.EncodeToString(argoImage))
}
// if DISABLE_DEFAULT_ARGOCD_CONSOLELINK env variable is true, Argo CD ConsoleLink will be deleted
func isConsoleLinkDisabled() bool {
return strings.ToLower(os.Getenv(common.DisableDefaultArgoCDConsoleLink)) == "true"
}
// SetupWithManager sets up the controller with the Manager.
func (r *ReconcileArgoCDRoute) SetupWithManager(mgr ctrl.Manager) error {
// Watch for changes to argocd-server route in the default argocd instance namespace
// The ConsoleLink holds the route URL and should be regenerated when route is updated
return ctrl.NewControllerManagedBy(mgr).
For(&routev1.Route{}, builder.WithPredicates(filterPredicate(filterArgoCDRoute))).
Complete(r)
}
func filterPredicate(assert func(namespace, name string) bool) predicate.Funcs {
return predicate.Funcs{
UpdateFunc: func(e event.UpdateEvent) bool {
return assert(e.ObjectNew.GetNamespace(), e.ObjectNew.GetName()) &&
e.ObjectNew.GetResourceVersion() != e.ObjectOld.GetResourceVersion()
},
CreateFunc: func(e event.CreateEvent) bool {
return assert(e.Object.GetNamespace(), e.Object.GetName())
},
DeleteFunc: func(e event.DeleteEvent) bool {
return assert(e.Object.GetNamespace(), e.Object.GetName())
},
}
}
func filterArgoCDRoute(namespace, name string) bool {
return namespace == argocdNS && argocdRouteName == name
}
// blank assignment to verify that ReconcileArgoCDRoute implements reconcile.Reconciler
var _ reconcile.Reconciler = &ReconcileArgoCDRoute{}
// ReconcileArgoCDRoute reconciles a ArgoCD Route object
type ReconcileArgoCDRoute struct {
// This client, initialized using mgr.Client() above, is a split client
// that reads objects from the cache and writes to the apiserver
Client client.Client
Scheme *runtime.Scheme
}
// Reconcile reads that state of the cluster for a ArgoCD Route object and makes changes based on the state read
// and what is in the Route.Spec
// Note:
// The Controller will requeue the Request to be processed again if the returned error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
func (r *ReconcileArgoCDRoute) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
var logs = logf.Log.WithName("controller_argocd_route")
reqLogger := logs.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
reqLogger.Info("Reconciling ArgoCD Route")
if !util.IsConsoleAPIFound() {
reqLogger.Info("Skip argocd route reconcile: OpenShift Console API not found")
return reconcile.Result{}, nil
}
// Fetch ArgoCD server route
argoCDRoute := &routev1.Route{}
err := r.Client.Get(ctx, types.NamespacedName{Name: argocdRouteName, Namespace: argocdNS}, argoCDRoute)
if err != nil {
if errors.IsNotFound(err) {
reqLogger.Info("ArgoCD server route not found", "Route.Namespace", argocdNS)
// if argocd-server route is deleted, remove the ConsoleLink if present
return reconcile.Result{}, r.deleteConsoleLinkIfPresent(ctx, reqLogger)
}
return reconcile.Result{}, err
}
reqLogger.Info("Route found for argocd-server", "Route.Host", argoCDRoute.Spec.Host)
argoCDRouteURL := fmt.Sprintf("https://%s", argoCDRoute.Spec.Host)
consoleLink := newConsoleLink(argoCDRouteURL, "Cluster Argo CD")
found := &console.ConsoleLink{}
err = r.Client.Get(ctx, types.NamespacedName{Name: consoleLink.Name}, found)
if err != nil {
if errors.IsNotFound(err) {
if !isConsoleLinkDisabled() {
reqLogger.Info("Creating a new ConsoleLink", "ConsoleLink.Name", consoleLink.Name)
return reconcile.Result{}, r.Client.Create(ctx, consoleLink)
}
}
reqLogger.Error(err, "ConsoleLink not found", "ConsoleLink.Name", consoleLink.Name)
return reconcile.Result{}, err
}
if isConsoleLinkDisabled() {
return reconcile.Result{}, r.deleteConsoleLinkIfPresent(ctx, reqLogger)
} else if found.Spec.Href != argoCDRouteURL {
reqLogger.Info("Updating the existing ConsoleLink", "ConsoleLink.Name", consoleLink.Name)
found.Spec.Href = argoCDRouteURL
return reconcile.Result{}, r.Client.Update(ctx, found)
}
reqLogger.Info("Skip reconcile: ConsoleLink already exists", "ConsoleLink.Name", consoleLink.Name)
return reconcile.Result{}, nil
}
func newConsoleLink(href, text string) *console.ConsoleLink {
return &console.ConsoleLink{
ObjectMeta: metav1.ObjectMeta{
Name: consoleLinkName,
},
Spec: console.ConsoleLinkSpec{
Link: console.Link{
Text: text,
Href: href,
},
Location: console.ApplicationMenu,
ApplicationMenu: &console.ApplicationMenuSpec{
Section: "OpenShift GitOps",
ImageURL: encodedArgoImage,
},
},
}
}
func (r *ReconcileArgoCDRoute) deleteConsoleLinkIfPresent(ctx context.Context, log logr.Logger) error {
err := r.Client.Get(ctx, types.NamespacedName{Name: consoleLinkName}, &console.ConsoleLink{})
if err != nil {
if errors.IsNotFound(err) {
return nil
}
return err
}
log.Info("Deleting ConsoleLink", "ConsoleLink.Name", consoleLinkName)
return r.Client.Delete(ctx, &console.ConsoleLink{ObjectMeta: metav1.ObjectMeta{Name: consoleLinkName}})
}
func imageDataURL(data string) string {
return fmt.Sprintf("data:image/png;base64,%s", data)
}