-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: remove pre 4.16 components from cluster on startup
Signed-off-by: Jakob Möller <[email protected]>
- Loading branch information
1 parent
0a16e1e
commit 5ae53f1
Showing
11 changed files
with
228 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package microlvms | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/openshift/lvm-operator/internal/cluster" | ||
appsv1 "k8s.io/api/apps/v1" | ||
coordinationv1 "k8s.io/api/coordination/v1" | ||
k8serrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/utils/ptr" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/client/apiutil" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
) | ||
|
||
const ( | ||
TopoLVMLegacyControllerName = "topolvm-controller" | ||
TopoLVMLegacyNodeDaemonSetName = "topolvm-node" | ||
) | ||
|
||
type Cleanup struct { | ||
namespace string | ||
client client.Client | ||
} | ||
|
||
func NewCleanup(client client.Client, namespace string) *Cleanup { | ||
return &Cleanup{ | ||
namespace: namespace, | ||
client: client, | ||
} | ||
} | ||
|
||
// RemovePreMicroLVMSComponents is a method of the `Cleanup` struct that performs cleanup tasks for the components | ||
// that ran pre MicroLVMS, e.g. separate controllers or daemonsets. | ||
func (c *Cleanup) RemovePreMicroLVMSComponents(ctx context.Context) error { | ||
objs := []client.Object{ | ||
// integrated into lvms operator | ||
&appsv1.Deployment{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: TopoLVMLegacyControllerName, | ||
Namespace: c.namespace, | ||
}, | ||
}, | ||
// integrated into vgmanager | ||
&appsv1.DaemonSet{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: TopoLVMLegacyNodeDaemonSetName, | ||
Namespace: c.namespace, | ||
}, | ||
}, | ||
// replaced by Replace Deployment strategy without Lease | ||
&coordinationv1.Lease{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: cluster.LeaseName, | ||
Namespace: c.namespace, | ||
}, | ||
}, | ||
} | ||
|
||
results := make(chan error, len(objs)) | ||
deleteImmediatelyIfExistsByIndex := func(i int) { | ||
results <- c.deleteImmediatelyIfExists(ctx, objs[i]) | ||
} | ||
for i := range objs { | ||
go deleteImmediatelyIfExistsByIndex(i) | ||
} | ||
|
||
var errs []error | ||
for i := 0; i < len(objs); i++ { | ||
if err := <-results; err != nil { | ||
errs = append(errs, err) | ||
} | ||
} | ||
|
||
if len(errs) > 0 { | ||
return fmt.Errorf("failed to run pre 4.16 MicroLVMS cleanup: %w", errors.Join(errs...)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *Cleanup) deleteImmediatelyIfExists(ctx context.Context, obj client.Object) error { | ||
gvk, _ := apiutil.GVKForObject(obj, c.client.Scheme()) | ||
logger := log.FromContext(ctx).WithValues("gvk", gvk.String(), | ||
"name", obj.GetName(), "namespace", obj.GetNamespace()) | ||
|
||
if err := c.client.Delete(ctx, obj, &client.DeleteOptions{ | ||
GracePeriodSeconds: ptr.To(int64(0)), | ||
}); err != nil { | ||
if k8serrors.IsNotFound(err) { | ||
logger.V(1).Info("not found, nothing to delete in cleanup.") | ||
return nil | ||
} | ||
return fmt.Errorf("cleanup delete failed: %w", err) | ||
} | ||
|
||
logger.Info("delete successful.") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package microlvms | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/openshift/lvm-operator/internal/cluster" | ||
appsv1 "k8s.io/api/apps/v1" | ||
coordinationv1 "k8s.io/api/coordination/v1" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
"sigs.k8s.io/controller-runtime/pkg/client/interceptor" | ||
) | ||
|
||
func TestRemovePreMicroLVMSComponents(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
exist bool | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "objects dont exist anymore (post-migration)", | ||
exist: false, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "objects still exist (pre-migration)", | ||
exist: true, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "objects exist but delete fails", | ||
exist: true, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
namespace := "openshift-storage" | ||
fakeClientBuilder := fake.NewClientBuilder(). | ||
WithScheme(setUpScheme()). | ||
WithObjects(setUpObjs(tt.exist, namespace)...) | ||
if tt.wantErr { | ||
fakeClientBuilder.WithInterceptorFuncs(interceptor.Funcs{ | ||
Delete: func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.DeleteOption) error { | ||
return errors.New("delete failed") | ||
}, | ||
}) | ||
} | ||
cleanup := NewCleanup(fakeClientBuilder.Build(), namespace) | ||
if err := cleanup.RemovePreMicroLVMSComponents(context.Background()); (err != nil) != tt.wantErr { | ||
t.Errorf("RemovePreMicroLVMSComponents() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func setUpScheme() *runtime.Scheme { | ||
scheme := runtime.NewScheme() | ||
_ = appsv1.AddToScheme(scheme) | ||
_ = coordinationv1.AddToScheme(scheme) | ||
return scheme | ||
} | ||
|
||
func setUpObjs(exist bool, namespace string) []client.Object { | ||
if exist { | ||
return nil | ||
} | ||
return []client.Object{ | ||
&appsv1.Deployment{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: TopoLVMLegacyControllerName, | ||
Namespace: namespace, | ||
}, | ||
}, | ||
&appsv1.DaemonSet{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: TopoLVMLegacyNodeDaemonSetName, | ||
Namespace: namespace, | ||
}, | ||
}, | ||
&coordinationv1.Lease{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: cluster.LeaseName, | ||
Namespace: namespace, | ||
}, | ||
}, | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters