Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add addon dependent api and doc #8053

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions apis/extensions/v1alpha1/addon_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ type AddonSpec struct {
//
// +optional
CliPlugins []CliPlugin `json:"cliPlugins,omitempty"`

// Specify all addons that this addon depends on.
// +optional
DependentAddons []DependentAddon `json:"dependentAddons,omitempty"`
}

// AddonStatus defines the observed state of an add-on.
Expand Down Expand Up @@ -319,6 +323,18 @@ type CliPlugin struct {
Description string `json:"description,omitempty"`
}

type DependentAddon struct {
// The name of the dependent addon.
//
// +kubebuilder:validation:Required
Name string `json:"name"`

// All matching versions of the dependent addon. If empty, defaults to the same version as the current addon.
//
// +optional
Version []string `json:"version"`
skyrise-l marked this conversation as resolved.
Show resolved Hide resolved
}

func (r *ResourceMappingItem) HasStorageMapping() bool {
return !(r == nil || r.Storage == "")
}
Expand Down
27 changes: 27 additions & 0 deletions apis/extensions/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions config/crd/bases/extensions.kubeblocks.io_addons.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,23 @@ spec:
type: object
minItems: 1
type: array
dependentAddons:
description: Specify all addons that this addon depends on.
items:
properties:
name:
description: The name of the dependent addon.
type: string
version:
description: All matching versions of the dependent addon. If
empty, defaults to the same version as the current addon.
items:
type: string
type: array
required:
- name
type: object
type: array
description:
description: Specifies the description of the add-on.
type: string
Expand Down
60 changes: 60 additions & 0 deletions controllers/extensions/addon_controller_stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,12 @@ func (r *installableCheckStage) Handle(ctx context.Context) {
return
}

if err := checkDependentAddon(addon, r.reconciler.Client); err != nil {
setAddonErrorConditions(ctx, &r.stageCtx, addon, true, true, AddonCheckDependencyError, err.Error())
r.setReconciled()
return
}

r.reqCtx.Log.V(1).Info("installableCheckStage", "phase", addon.Status.Phase)

// check the annotations constraint about Kubeblocks Version
Expand Down Expand Up @@ -1259,6 +1265,60 @@ func checkAddonSpec(addon *extensionsv1alpha1.Addon) error {
return nil
}

func checkDependentAddon(addon *extensionsv1alpha1.Addon, cli client.Client) error {
if len(addon.Spec.DependentAddons) == 0 {
return nil
}
for _, dependency := range addon.Spec.DependentAddons {
versions := dependency.Version
if len(versions) == 0 {
currentVersion, ok := addon.Labels[AddonVersion]
if !ok {
return fmt.Errorf("dependent addon version is nil and current addon does not have a version label")
}
versions = []string{currentVersion}
}
found := false

// Retrieve the dependent addon object
dependentAddon := &extensionsv1alpha1.Addon{}
if err := cli.Get(context.TODO(), client.ObjectKey{Name: dependency.Name, Namespace: addon.Namespace}, dependentAddon); err != nil {
if apierrors.IsNotFound(err) {
return fmt.Errorf("dependent addon %s not found", dependency.Name)
}
return err
}

// Get the version of the dependent addon
depAddonVersionStr, ok := dependentAddon.Labels[AddonVersion]
if !ok {
return fmt.Errorf("dependent addon %s does not have a version label", dependency.Name)
}
depAddonVersion, err := semver.NewVersion(depAddonVersionStr)
if err != nil {
return fmt.Errorf("invalid version %s for addon %s: %v", depAddonVersionStr, dependency.Name, err)
}

// Check if the dependent addon's version satisfies any of the specified constraints
for _, versionConstraint := range versions {
// Parse the version constraint
constraint, err := semver.NewConstraint(versionConstraint)
if err != nil {
return fmt.Errorf("invalid version constraint %s: %v", versionConstraint, err)
}
// Check if the version satisfies the constraint
if constraint.Check(depAddonVersion) {
found = true
break
}
}
if !found {
return fmt.Errorf("dependent addon %s does not satisfy any of the specified version constraints %v", dependency.Name, versions)
}
}
return nil
}

func setAddonProviderAndVersion(ctx context.Context, stageCtx *stageCtx, addon *extensionsv1alpha1.Addon) {
// if not set provider and version in spec, set it from labels
if addon.Labels == nil {
Expand Down
47 changes: 47 additions & 0 deletions controllers/extensions/addon_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ var _ = Describe("Addon controller", func() {

Context("Addon controller test", func() {
var addon *extensionsv1alpha1.Addon
var dependAddon *extensionsv1alpha1.Addon
var key types.NamespacedName
var clusterKey types.NamespacedName
BeforeEach(func() {
Expand Down Expand Up @@ -577,6 +578,52 @@ var _ = Describe("Addon controller", func() {
}).Should(Succeed())
})

It("should successfully reconcile a custom resource for Addon install with dependency addon", func() {
By("By create addon with not installed dependent addon")
createAddonSpecWithRequiredAttributes(func(newOjb *extensionsv1alpha1.Addon) {
newOjb.Spec.Installable.AutoInstall = true
newOjb.Spec.DependentAddons = []extensionsv1alpha1.DependentAddon{
{
Name: "addon-test-123",
Version: []string{">=1.0.0"},
},
}
})

By("By enable addon should failed")
Eventually(func(g Gomega) {
doReconcileOnce(g)
addon = &extensionsv1alpha1.Addon{}
g.Expect(testCtx.Cli.Get(ctx, key, addon)).To(Not(HaveOccurred()))
g.Expect(addon.Status.Phase).Should(Equal(extensionsv1alpha1.AddonFailed))
g.Expect(addon.Spec.InstallSpec).Should(BeNil())
g.Expect(addon.Status.ObservedGeneration).Should(BeEquivalentTo(1))
}).Should(Succeed())

By("By install dependent addon")
modifiers := func(newObj *extensionsv1alpha1.Addon) {
newObj.Spec.Installable.AutoInstall = true
newObj.Name = "addon-test-123"
newObj.SetLabels(map[string]string{AddonVersion: "2.0.0"})
}

dependAddon = testapps.CreateCustomizedObj(&testCtx, "addon/addon.yaml", &extensionsv1alpha1.Addon{}, modifiers)
Expect(dependAddon.Spec.DefaultInstallValues).ShouldNot(BeEmpty())

By("By enable addon when the dependent addon is installed")
createAddonSpecWithRequiredAttributes(func(newOjb *extensionsv1alpha1.Addon) {
newOjb.Spec.Installable.AutoInstall = true
newOjb.Spec.DependentAddons = []extensionsv1alpha1.DependentAddon{
{
Name: "addon-test-123",
Version: []string{">=1.0.0"},
},
}
})
enablingPhaseCheck(2)
fakeInstallationFailedJob(2)
})

It("should successfully reconcile a custom resource for Addon with autoInstall=true with status.phase=Failed", func() {
createAutoInstallAddon()

Expand Down
1 change: 1 addition & 0 deletions controllers/extensions/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const (
UninstallationFailedLogs = "UninstallationFailedLogs"
AddonRefObjError = "ReferenceObjectError"
AddonCheckError = "AddonCheckError"
AddonCheckDependencyError = "AddonCheckDependencyError"

// config keys used in viper
maxConcurrentReconcilesKey = "MAXCONCURRENTRECONCILES_ADDON"
Expand Down
17 changes: 17 additions & 0 deletions deploy/helm/crds/extensions.kubeblocks.io_addons.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,23 @@ spec:
type: object
minItems: 1
type: array
dependentAddons:
description: Specify all addons that this addon depends on.
items:
properties:
name:
description: The name of the dependent addon.
type: string
version:
description: All matching versions of the dependent addon. If
empty, defaults to the same version as the current addon.
items:
type: string
type: array
required:
- name
type: object
type: array
description:
description: Specifies the description of the add-on.
type: string
Expand Down
68 changes: 68 additions & 0 deletions docs/developer_docs/api-reference/add-on.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,20 @@ the selector and auto-install settings.</p>
<p>Specifies the CLI plugin installation specifications.</p>
</td>
</tr>
<tr>
<td>
<code>dependentAddons</code><br/>
<em>
<a href="#extensions.kubeblocks.io/v1alpha1.DependentAddon">
[]DependentAddon
</a>
</em>
</td>
<td>
<em>(Optional)</em>
<p>Specify all addons that this addon depends on.</p>
</td>
</tr>
</table>
</td>
</tr>
Expand Down Expand Up @@ -624,6 +638,20 @@ the selector and auto-install settings.</p>
<p>Specifies the CLI plugin installation specifications.</p>
</td>
</tr>
<tr>
<td>
<code>dependentAddons</code><br/>
<em>
<a href="#extensions.kubeblocks.io/v1alpha1.DependentAddon">
[]DependentAddon
</a>
</em>
</td>
<td>
<em>(Optional)</em>
<p>Specify all addons that this addon depends on.</p>
</td>
</tr>
</tbody>
</table>
<h3 id="extensions.kubeblocks.io/v1alpha1.AddonStatus">AddonStatus
Expand Down Expand Up @@ -794,6 +822,46 @@ string
</tr>
</tbody>
</table>
<h3 id="extensions.kubeblocks.io/v1alpha1.DependentAddon">DependentAddon
</h3>
<p>
(<em>Appears on:</em><a href="#extensions.kubeblocks.io/v1alpha1.AddonSpec">AddonSpec</a>)
</p>
<div>
</div>
<table>
<thead>
<tr>
<th>Field</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<code>name</code><br/>
<em>
string
</em>
</td>
<td>
<p>The name of the dependent addon.</p>
</td>
</tr>
<tr>
<td>
<code>version</code><br/>
<em>
[]string
</em>
</td>
<td>
<em>(Optional)</em>
<p>All matching versions of the dependent addon. If empty, defaults to the same version as the current addon.</p>
</td>
</tr>
</tbody>
</table>
<h3 id="extensions.kubeblocks.io/v1alpha1.HelmInstallOptions">HelmInstallOptions
(<code>map[string]string</code> alias)</h3>
<p>
Expand Down
22 changes: 21 additions & 1 deletion i18n/zh-cn/developer-docs/api-reference/add-on.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ Resource Types:
<tr><td><code>defaultInstallValues</code><br/><em><a href="#extensions.kubeblocks.io/v1alpha1.AddonDefaultInstallSpecItem">[]AddonDefaultInstallSpecItem</a></em></td><td><p>Default installation parameters.</p><br /></td></tr>
<tr><td><code>install</code><br/><em><a href="#extensions.kubeblocks.io/v1alpha1.AddonInstallSpec">AddonInstallSpec</a></em></td><td><em>(Optional)</em><p>Installation parameters.</p><br /></td></tr>
<tr><td><code>installable</code><br/><em><a href="#extensions.kubeblocks.io/v1alpha1.InstallableSpec">InstallableSpec</a></em></td><td><em>(Optional)</em><p>Addon installable spec. It provides selector and auto-install settings.</p><br /></td></tr>
<tr><td><code>cliPlugins</code><br/><em><a href="#extensions.kubeblocks.io/v1alpha1.CliPlugin">[]CliPlugin</a></em></td><td><em>(Optional)</em><p>Plugin installation spec.</p><br /></td></tr></table></td></tr>
<tr><td><code>cliPlugins</code><br/><em><a href="#extensions.kubeblocks.io/v1alpha1.CliPlugin">[]CliPlugin</a></em></td><td><em>(Optional)</em><p>Plugin installation spec.</p><br /></td></tr>
<tr><td><code>DependentAddons</code><br/><em><a href="#extensions.kubeblocks.io/v1alpha1.DependentAddon">[]DependentAddon</a></em></td><td><em>(Optional)</em><p>Addon Dependency spec.</p><br /></td></tr></table></td></tr></table></td></tr>
<tr><td><code>status</code><br/><em><a href="#extensions.kubeblocks.io/v1alpha1.AddonStatus">AddonStatus</a></em></td><td></td></tr>
</tbody>
</table>
Expand Down Expand Up @@ -183,6 +184,7 @@ Resource Types:
<tr><td><code>install</code><br/><em><a href="#extensions.kubeblocks.io/v1alpha1.AddonInstallSpec">AddonInstallSpec</a></em></td><td><em>(Optional)</em><p>Installation parameters.</p><br /></td></tr>
<tr><td><code>installable</code><br/><em><a href="#extensions.kubeblocks.io/v1alpha1.InstallableSpec">InstallableSpec</a></em></td><td><em>(Optional)</em><p>Addon installable spec. It provides selector and auto-install settings.</p><br /></td></tr>
<tr><td><code>cliPlugins</code><br/><em><a href="#extensions.kubeblocks.io/v1alpha1.CliPlugin">[]CliPlugin</a></em></td><td><em>(Optional)</em><p>Plugin installation spec.</p><br /></td></tr>
<tr><td><code>DependentAddons</code><br/><em><a href="#extensions.kubeblocks.io/v1alpha1.DependentAddon">[]DependentAddon</a></em></td><td><em>(Optional)</em><p>Addon Dependency spec.</p><br /></td></tr>
</tbody>
</table>
<h3 id="extensions.kubeblocks.io/v1alpha1.AddonStatus">AddonStatus</h3>
Expand Down Expand Up @@ -242,6 +244,24 @@ Resource Types:
<tr><td><code>description</code><br/><em>string</em></td><td><em>(Optional)</em><p>The description of the plugin.</p><br /></td></tr>
</tbody>
</table>
<h3 id="extensions.kubeblocks.io/v1alpha1.DependentAddon">DependentAddon</h3>
<p>
(<em>Appears on:</em><a href="#extensions.kubeblocks.io/v1alpha1.AddonSpec">AddonSpec</a>)
</p>
<div>
</div>
<table>
<thead>
<tr>
<th>Field</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr><td><code>name</code><br/><em>string</em></td><td><p>Name of the dependent addon.</p><br /></td></tr>
<tr><td><code>version</code><br/><em>string[]</em></td><td><em>(Optional)</em><p>All matching versions of the dependent addon. Default to the current addon version</p><br /></td></tr>
</tbody>
</table>
<h3 id="extensions.kubeblocks.io/v1alpha1.DataObjectKeySelector">DataObjectKeySelector</h3>
<p>
(<em>Appears on:</em><a href="#extensions.kubeblocks.io/v1alpha1.HelmInstallValues">HelmInstallValues</a>)
Expand Down
Loading