Skip to content

Commit

Permalink
feat: check pre-requisites on OSSM plugin init (#7)
Browse files Browse the repository at this point in the history
* add prereq checks to init, add tests
---------

Co-authored-by: Bartosz Majsak <[email protected]>
  • Loading branch information
cam-garrison and bartoszmajsak authored Aug 8, 2023
1 parent b47ac7c commit f84e4d3
Show file tree
Hide file tree
Showing 6 changed files with 10,465 additions and 1 deletion.
47 changes: 47 additions & 0 deletions pkg/kfapp/ossm/k8s_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,50 @@ func (o *OssmInstaller) PatchResourceFromFile(filename string, elems ...configty
}
return nil
}

func (o *OssmInstaller) VerifyCRDInstalled(group string, version string, resource string) error {
dynamicClient, err := dynamic.NewForConfig(o.config)
if err != nil {
log.Error(err, "Failed to initialize dynamic client")
return err
}

crdGVR := schema.GroupVersionResource{
Group: group,
Version: version,
Resource: resource,
}

_, err = dynamicClient.Resource(crdGVR).List(context.Background(), metav1.ListOptions{})
return err
}

func (o *OssmInstaller) CheckSMCPStatus(name, namespace string) (string, error) {
dynamicClient, err := dynamic.NewForConfig(o.config)
if err != nil {
log.Info("Failed to initialize dynamic client")
return "", err
}

gvr := schema.GroupVersionResource{
Group: "maistra.io",
Version: "v1",
Resource: "servicemeshcontrolplanes",
}

unstructObj, err := dynamicClient.Resource(gvr).Namespace(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
log.Info("Failed to find SMCP")
return "", err
}

conditions, found, err := unstructured.NestedSlice(unstructObj.Object, "status", "conditions")
if err != nil || !found {
log.Info("status conditions not found or error in parsing of SMCP")
return "", err
}
lastCondition := conditions[len(conditions)-1].(map[string]interface{})
status := lastCondition["type"].(string)

return status, nil
}
28 changes: 27 additions & 1 deletion pkg/kfapp/ossm/ossm_installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ func (o *OssmInstaller) Init(_ kftypesv3.ResourceEnum) error {
return internalError(errors.New(reason))
}

// TODO ensure operators are installed
if err := o.VerifyCRDInstalled("operator.authorino.kuadrant.io", "v1beta1", "authorinos"); err != nil {
log.Info("Failed to find the pre-requisite authorinos CRD, please ensure Authorino operator is installed.")
return internalError(err)
}
if err := o.ensureServiceMeshInstalled(pluginSpec); err != nil {
return internalError(err)
}

if err := o.createResourceTracker(); err != nil {
return internalError(err)
Expand Down Expand Up @@ -233,6 +239,26 @@ func (o *OssmInstaller) MigrateDataScienceProjects() error {
return result.ErrorOrNil()
}

func (o *OssmInstaller) ensureServiceMeshInstalled(pluginSpec *ossmplugin.OssmPluginSpec) error {
if err := o.VerifyCRDInstalled("maistra.io", "v2", "servicemeshcontrolplanes"); err != nil {
log.Info("Failed to find the pre-requisite SMCP CRD, please ensure OSSM operator is installed.")
return internalError(err)
}
smcp := pluginSpec.Mesh.Name
smcpNs := pluginSpec.Mesh.Namespace

status, err := o.CheckSMCPStatus(smcp, smcpNs)
if err != nil {
log.Info("An error occurred while checking SMCP status - ensure the SMCP referenced exists.")
return internalError(err)
}
if status != "Ready" {
log.Info("The referenced SMCP is not ready.", "SMCP name", smcp, "SMCP NS", smcpNs)
return internalError(errors.New("SMCP status is not ready"))
}
return nil
}

func internalError(err error) error {
return &kfapisv3.KfError{
Code: int(kfapisv3.INTERNAL_ERROR),
Expand Down
Loading

0 comments on commit f84e4d3

Please sign in to comment.