Skip to content

Commit ed577c9

Browse files
committed
Handle case where IstioRevisionTag is modified to point to a revision in a different namespace
Signed-off-by: Marko Lukša <[email protected]>
1 parent 0095025 commit ed577c9

File tree

1 file changed

+44
-44
lines changed

1 file changed

+44
-44
lines changed

controllers/istiobase/istiobase_controller.go

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -88,43 +88,35 @@ func (r *Reconciler) Reconcile(ctx context.Context, _ reconcile.Request) (reconc
8888
func (r *Reconciler) doReconcile(ctx context.Context) error {
8989
log := logf.FromContext(ctx)
9090

91+
currNamespace, err := r.findIstioBaseNamespace(ctx)
92+
if err != nil {
93+
return err
94+
}
95+
9196
rev, err := r.getDefaultRevision(ctx)
9297
if err != nil {
9398
if errors.IsNotFound(err) {
94-
if err := r.uninstallHelmChart(ctx); err != nil {
95-
return fmt.Errorf("failed to uninstall base chart: %w", err)
99+
if currNamespace != "" {
100+
log.Info("Uninstalling Helm chart")
101+
_, err := r.ChartManager.UninstallChart(ctx, baseReleaseName, currNamespace)
102+
if err != nil {
103+
return fmt.Errorf("failed to uninstall Helm chart %q: %w", baseChartName, err)
104+
}
96105
}
97106
return nil
98107
}
99108
return fmt.Errorf("failed to reconcile: %w", err)
100109
}
101-
log.Info("Installing Helm chart")
102-
return r.installHelmChart(ctx, rev)
103-
}
104110

105-
func (r *Reconciler) getDefaultRevision(ctx context.Context) (*v1.IstioRevision, error) {
106-
// 1. get the IstioRevision referenced in the default IstioRevisionTag
107-
tag := v1.IstioRevisionTag{}
108-
if err := r.Client.Get(ctx, types.NamespacedName{Name: "default"}, &tag); err != nil {
109-
if !errors.IsNotFound(err) {
110-
return nil, fmt.Errorf("failed to get default IstioRevisionTag: %w", err)
111+
if rev.Spec.Namespace != currNamespace {
112+
log.Info("Uninstalling Helm chart from previous namespace")
113+
_, err := r.ChartManager.UninstallChart(ctx, baseReleaseName, currNamespace)
114+
if err != nil {
115+
return fmt.Errorf("failed to uninstall Helm chart %q: %w", baseChartName, err)
111116
}
112117
}
113118

114-
revName := tag.Status.IstioRevision
115-
if revName == "" {
116-
revName = "default"
117-
}
118-
119-
// 2. get the IstioRevision
120-
rev := &v1.IstioRevision{}
121-
if err := r.Client.Get(ctx, types.NamespacedName{Name: revName}, rev); err != nil {
122-
return rev, fmt.Errorf("failed to get IstioRevision %q: %w", revName, err)
123-
}
124-
return rev, nil
125-
}
126-
127-
func (r *Reconciler) installHelmChart(ctx context.Context, rev *v1.IstioRevision) error {
119+
log.Info("Installing Helm chart")
128120
version, err := istioversion.Resolve(rev.Spec.Version)
129121
if err != nil {
130122
return fmt.Errorf("failed to resolve IstioRevision version for %q: %w", rev.Name, err)
@@ -142,37 +134,45 @@ func (r *Reconciler) installHelmChart(ctx context.Context, rev *v1.IstioRevision
142134
return nil
143135
}
144136

145-
func (r *Reconciler) getChartDir(version string) string {
146-
return path.Join(r.Config.ResourceDirectory, version, "charts", baseChartName)
147-
}
148-
149-
func (r *Reconciler) uninstallHelmChart(ctx context.Context) error {
150-
log := logf.FromContext(ctx)
137+
// findIstioBaseNamespace returns the namespace the istio base chart is installed in. If the chart is not installed, it returns an empty string.
138+
func (r *Reconciler) findIstioBaseNamespace(ctx context.Context) (string, error) {
151139
releases, err := r.ChartManager.ListReleases(ctx)
152140
if err != nil {
153-
return fmt.Errorf("failed to check whether Helm chart %q is installed: %w", baseChartName, err)
141+
return "", fmt.Errorf("failed to check whether Helm chart %q is installed: %w", baseChartName, err)
154142
}
155143

156-
namespace := ""
157144
for _, release := range releases {
158145
if release.Name == baseReleaseName {
159-
namespace = release.Namespace
160-
break
146+
return release.Namespace, nil
161147
}
162148
}
149+
return "", nil
150+
}
163151

164-
if namespace == "" {
165-
log.V(3).Info(fmt.Sprintf("Helm release %q not found", baseReleaseName))
166-
return nil
152+
func (r *Reconciler) getDefaultRevision(ctx context.Context) (*v1.IstioRevision, error) {
153+
// 1. get the IstioRevision referenced in the default IstioRevisionTag
154+
tag := v1.IstioRevisionTag{}
155+
if err := r.Client.Get(ctx, types.NamespacedName{Name: "default"}, &tag); err != nil {
156+
if !errors.IsNotFound(err) {
157+
return nil, fmt.Errorf("failed to get default IstioRevisionTag: %w", err)
158+
}
167159
}
168160

169-
log.V(3).Info(fmt.Sprintf("Helm release %q found in namespace %q", baseReleaseName, namespace))
170-
log.Info("Uninstalling Helm chart")
171-
_, err = r.ChartManager.UninstallChart(ctx, baseReleaseName, namespace)
172-
if err != nil {
173-
return fmt.Errorf("failed to uninstall Helm chart %q: %w", baseChartName, err)
161+
revName := tag.Status.IstioRevision
162+
if revName == "" {
163+
revName = "default"
174164
}
175-
return nil
165+
166+
// 2. get the IstioRevision
167+
rev := &v1.IstioRevision{}
168+
if err := r.Client.Get(ctx, types.NamespacedName{Name: revName}, rev); err != nil {
169+
return rev, fmt.Errorf("failed to get IstioRevision %q: %w", revName, err)
170+
}
171+
return rev, nil
172+
}
173+
174+
func (r *Reconciler) getChartDir(version string) string {
175+
return path.Join(r.Config.ResourceDirectory, version, "charts", baseChartName)
176176
}
177177

178178
// SetupWithManager sets up the controller with the Manager.

0 commit comments

Comments
 (0)