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 6 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
AddonDependencies []AddonDependency `json:"addonDependencies,omitempty"`
skyrise-l marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does it mean for a dependency to have an addon's name and version? Based on the issue corresponding to the PR, apecloud-mysql actually depends on a specific version of the etcd cmpd name, but the etcd addon name is not equal to the etcd cmpd name.

}

// 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 AddonDependency 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 @@ -62,6 +62,23 @@ spec:
spec:
description: AddonSpec defines the desired state of an add-on.
properties:
addonDependencies:
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
cliPlugins:
description: Specifies the CLI plugin installation specifications.
items:
Expand Down
42 changes: 42 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 := checkAddonDependency(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,42 @@ func checkAddonSpec(addon *extensionsv1alpha1.Addon) error {
return nil
}

func checkAddonDependency(addon *extensionsv1alpha1.Addon, cli client.Client) error {
if len(addon.Spec.AddonDependencies) == 0 {
return nil
}
for _, dependency := range addon.Spec.AddonDependencies {
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
for _, version := range versions {
// If the Addon with the given name is not found, move to the next version
dependentAddon := &extensionsv1alpha1.Addon{}
if err := cli.Get(context.TODO(), client.ObjectKey{Name: dependency.Name, Namespace: addon.Namespace}, dependentAddon); err != nil {
if apierrors.IsNotFound(err) {
continue
}
return err
}
// Check if the version matches
if val, ok := dependentAddon.Labels[AddonVersion]; ok && val == version {
found = true
break
}
}
if !found {
return fmt.Errorf("dependency %s with any of the specified versions %v not found", dependency.Name, dependency.Version)
}
}
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.AddonDependencies = []extensionsv1alpha1.AddonDependency{
{
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: "1.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.AddonDependencies = []extensionsv1alpha1.AddonDependency{
{
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 @@ -62,6 +62,23 @@ spec:
spec:
description: AddonSpec defines the desired state of an add-on.
properties:
addonDependencies:
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
cliPlugins:
description: Specifies the CLI plugin installation specifications.
items:
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>addonDependencies</code><br/>
<em>
<a href="#extensions.kubeblocks.io/v1alpha1.AddonDependency">
[]AddonDependency
</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 @@ -258,6 +272,46 @@ all selectors must evaluate to true.</p>
</tr>
</tbody>
</table>
<h3 id="extensions.kubeblocks.io/v1alpha1.AddonDependency">AddonDependency
</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.AddonInstallExtraItem">AddonInstallExtraItem
</h3>
<p>
Expand Down Expand Up @@ -624,6 +678,20 @@ the selector and auto-install settings.</p>
<p>Specifies the CLI plugin installation specifications.</p>
</td>
</tr>
<tr>
<td>
<code>addonDependencies</code><br/>
<em>
<a href="#extensions.kubeblocks.io/v1alpha1.AddonDependency">
[]AddonDependency
</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
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>addonDependencies</code><br/><em><a href="#extensions.kubeblocks.io/v1alpha1.AddonDependency">[]AddonDependency</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>addonDependencies</code><br/><em><a href="#extensions.kubeblocks.io/v1alpha1.AddonDependency">[]AddonDependency</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.AddonDependency">AddonDependency</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