Skip to content

Commit

Permalink
update editable values api
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadhamzh committed Sep 11, 2024
1 parent c0bf02c commit 2f98897
Show file tree
Hide file tree
Showing 31 changed files with 364 additions and 115 deletions.
130 changes: 130 additions & 0 deletions modules/api/cmd/kubermatic-api/swagger.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion modules/api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ require (
google.golang.org/api v0.187.0
gopkg.in/yaml.v3 v3.0.1
k8c.io/kubeone v1.7.3
k8c.io/kubermatic/v2 v2.25.1-0.20240906153428-c4c2eccec9d0
k8c.io/kubermatic/v2 v2.26.0-beta.0.0.20240911164532-12912897fd2e
k8c.io/operating-system-manager v1.5.1-0.20240822183214-db378951daf3
k8c.io/reconciler v0.5.0
k8s.io/api v0.31.0
Expand Down
4 changes: 2 additions & 2 deletions modules/api/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -955,8 +955,8 @@ honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
k8c.io/kubeone v1.7.3 h1:KZ2Q6LQMxoiFf9UQ3ugqjid39NccduhJ50bdbP5tdIU=
k8c.io/kubeone v1.7.3/go.mod h1:9v2VFz/+l36cW65kd5YufEYHunbKlJ6P8SBakj05xgM=
k8c.io/kubermatic/v2 v2.25.1-0.20240906153428-c4c2eccec9d0 h1:QJeWngHDCez/A10QrNlnVhqSwtQ7bHwp+FzP+GRSmyg=
k8c.io/kubermatic/v2 v2.25.1-0.20240906153428-c4c2eccec9d0/go.mod h1:lrGS5QBish0sIsI+dqnV9RhZFZiXR/2QyNVVKe+90pU=
k8c.io/kubermatic/v2 v2.26.0-beta.0.0.20240911164532-12912897fd2e h1:iyoNuHGNQZQvHClSAa9cDFTK7UjcTjoxUfkMyOq1lgY=
k8c.io/kubermatic/v2 v2.26.0-beta.0.0.20240911164532-12912897fd2e/go.mod h1:LqIb4Cd7lTpQAj894cNG69Me/LLrS8P2eghB82OXl+8=
k8c.io/operating-system-manager v1.5.1-0.20240822183214-db378951daf3 h1:A9V4pXMVwWpmcyX6vq/B81rxIf/BeIjcv09K9z35A+0=
k8c.io/operating-system-manager v1.5.1-0.20240822183214-db378951daf3/go.mod h1:bRyRLDw3RRSQ+rOcoOfXKwwJTfr4IlvJhUn2M60+iJU=
k8c.io/reconciler v0.5.0 h1:BHpelg1UfI/7oBFctqOq8sX6qzflXpl3SlvHe7e8wak=
Expand Down
12 changes: 12 additions & 0 deletions modules/api/pkg/api/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ type PresetBodyMetadata struct {
type PresetProvider struct {
Name kubermaticv1.ProviderType `json:"name"`
Enabled bool `json:"enabled"`
IsCustomizable bool `json:"isEditable"`
VMwareCloudDirector *VMwareCloudDirectorAPIPreset `json:"vmwareCloudDirector,omitempty"`
OpenStack *OpenStackAPIPreset `json:"OpenStack,omitempty"`
}

// VMwareCloudDirectorPreset represents a preset for VMware Cloud Director
Expand All @@ -169,6 +171,16 @@ type VMwareCloudDirectorAPIPreset struct {
OVDCNetworks []string `json:"ovdcNetworks,omitempty"`
}

// OpenStack represents a preset for OpenStack Cloud Director
// swagger:model OpenStackAPIPreset
type OpenStackAPIPreset struct {
Network string `json:"network,omitempty"`
SecurityGroups string `json:"securityGroups,omitempty"`
FloatingIPPool string `json:"floatingIPPool,omitempty"`
RouterID string `json:"routerID,omitempty"`
SubnetID string `json:"subnetID,omitempty"`
}

// PresetStats represents a preset statistics
// swagger:model PresetStats
type PresetStats struct {
Expand Down
14 changes: 12 additions & 2 deletions modules/api/pkg/handler/common/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,19 @@ func GenerateCluster(
if err != nil {
return nil, utilerrors.NewBadRequest("invalid credentials: %v", err)
}

preset, err := credentialManager.GetPreset(ctx, adminUserInfo, &projectID, credentialName)
if err != nil {
return nil, err
}
// At the moment, the only provider that can be customized is OpenStack.
if body.Cluster.Spec.Cloud.Openstack != nil && preset.Spec.Openstack.IsCustomizable {
body.Cluster.Credential = ""
} else {
partialCluster.Labels[kubermaticv1.IsCredentialPresetLabelKey] = "true"
partialCluster.Annotations[kubermaticv1.PresetNameAnnotation] = credentialName
}
body.Cluster.Spec.Cloud = *cloudSpec
partialCluster.Labels[kubermaticv1.IsCredentialPresetLabelKey] = "true"
partialCluster.Annotations[kubermaticv1.PresetNameAnnotation] = credentialName
}

// Fetch the defaulting ClusterTemplate.
Expand Down
10 changes: 10 additions & 0 deletions modules/api/pkg/handler/v2/preset/preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,16 @@ func newAPIPreset(preset *kubermaticv1.Preset, enabled bool) apiv2.Preset {
}
}

if providerType == kubermaticv1.OpenstackCloudProvider && preset.Spec.Openstack.IsCustomizable {
provider.OpenStack = &apiv2.OpenStackAPIPreset{
Network: preset.Spec.Openstack.Network,
SecurityGroups: preset.Spec.Openstack.SecurityGroups,
FloatingIPPool: preset.Spec.Openstack.FloatingIPPool,
RouterID: preset.Spec.Openstack.RouterID,
SubnetID: preset.Spec.Openstack.SubnetID,
}
}

providers = append(providers, provider)
}
}
Expand Down
1 change: 0 additions & 1 deletion modules/api/pkg/handler/v2/provider/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,6 @@ func DecodeOpenstackSubnetReq(c context.Context, r *http.Request) (interface{},
if err != nil {
return nil, err
}

networkID := r.URL.Query().Get("network_id")
if networkID == "" {
return nil, fmt.Errorf("'network_id' is a required parameter and may not be empty")
Expand Down
14 changes: 8 additions & 6 deletions modules/api/pkg/provider/kubernetes/preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,17 +383,19 @@ func (m *PresetProvider) setOpenStackCredentials(preset *kubermaticv1.Preset, cl
cloud.Openstack.ApplicationCredentialID = credentials.ApplicationCredentialID
cloud.Openstack.ApplicationCredentialSecret = credentials.ApplicationCredentialSecret

cloud.Openstack.SubnetID = credentials.SubnetID
cloud.Openstack.Network = credentials.Network
cloud.Openstack.FloatingIPPool = credentials.FloatingIPPool
// if the preset is customizable then no need to update these value from the prest, since we have them in the req.body
if !preset.Spec.Openstack.IsCustomizable {
cloud.Openstack.SubnetID = credentials.SubnetID
cloud.Openstack.Network = credentials.Network
cloud.Openstack.RouterID = credentials.RouterID
cloud.Openstack.FloatingIPPool = credentials.FloatingIPPool
cloud.Openstack.SecurityGroups = credentials.SecurityGroups
}

if cloud.Openstack.FloatingIPPool == "" && dc.Spec.Openstack != nil && dc.Spec.Openstack.EnforceFloatingIP {
return nil, fmt.Errorf("preset error, no floating ip pool specified for OpenStack")
}

cloud.Openstack.RouterID = credentials.RouterID
cloud.Openstack.SecurityGroups = credentials.SecurityGroups

return &cloud, nil
}

Expand Down
102 changes: 0 additions & 102 deletions modules/api/pkg/resources/reconciling/zz_generated_reconcile.go
Original file line number Diff line number Diff line change
@@ -1,102 +0,0 @@
/*
Copyright 2024 The Kubermatic Kubernetes Platform contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package reconciling

import (
"context"
"fmt"

"k8c.io/reconciler/pkg/reconciling"
"k8s.io/apimachinery/pkg/types"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"

instancetypev1alpha1 "kubevirt.io/api/instancetype/v1alpha1"
)

// VirtualMachineInstancetypeReconciler defines an interface to create/update VirtualMachineInstancetypes.
type VirtualMachineInstancetypeReconciler = func(existing *instancetypev1alpha1.VirtualMachineInstancetype) (*instancetypev1alpha1.VirtualMachineInstancetype, error)

// NamedVirtualMachineInstancetypeReconcilerFactory returns the name of the resource and the corresponding Reconciler function.
type NamedVirtualMachineInstancetypeReconcilerFactory = func() (name string, reconciler VirtualMachineInstancetypeReconciler)

// VirtualMachineInstancetypeObjectWrapper adds a wrapper so the VirtualMachineInstancetypeReconciler matches ObjectReconciler.
// This is needed as Go does not support function interface matching.
func VirtualMachineInstancetypeObjectWrapper(reconciler VirtualMachineInstancetypeReconciler) reconciling.ObjectReconciler {
return func(existing ctrlruntimeclient.Object) (ctrlruntimeclient.Object, error) {
if existing != nil {
return reconciler(existing.(*instancetypev1alpha1.VirtualMachineInstancetype))
}
return reconciler(&instancetypev1alpha1.VirtualMachineInstancetype{})
}
}

// ReconcileVirtualMachineInstancetypes will create and update the VirtualMachineInstancetypes coming from the passed VirtualMachineInstancetypeReconciler slice.
func ReconcileVirtualMachineInstancetypes(ctx context.Context, namedFactories []NamedVirtualMachineInstancetypeReconcilerFactory, namespace string, client ctrlruntimeclient.Client, objectModifiers ...reconciling.ObjectModifier) error {
for _, factory := range namedFactories {
name, reconciler := factory()
reconcileObject := VirtualMachineInstancetypeObjectWrapper(reconciler)
reconcileObject = reconciling.CreateWithNamespace(reconcileObject, namespace)
reconcileObject = reconciling.CreateWithName(reconcileObject, name)

for _, objectModifier := range objectModifiers {
reconcileObject = objectModifier(reconcileObject)
}

if err := reconciling.EnsureNamedObject(ctx, types.NamespacedName{Namespace: namespace, Name: name}, reconcileObject, client, &instancetypev1alpha1.VirtualMachineInstancetype{}, false); err != nil {
return fmt.Errorf("failed to ensure VirtualMachineInstancetype %s/%s: %w", namespace, name, err)
}
}

return nil
}

// VirtualMachinePreferenceReconciler defines an interface to create/update VirtualMachinePreferences.
type VirtualMachinePreferenceReconciler = func(existing *instancetypev1alpha1.VirtualMachinePreference) (*instancetypev1alpha1.VirtualMachinePreference, error)

// NamedVirtualMachinePreferenceReconcilerFactory returns the name of the resource and the corresponding Reconciler function.
type NamedVirtualMachinePreferenceReconcilerFactory = func() (name string, reconciler VirtualMachinePreferenceReconciler)

// VirtualMachinePreferenceObjectWrapper adds a wrapper so the VirtualMachinePreferenceReconciler matches ObjectReconciler.
// This is needed as Go does not support function interface matching.
func VirtualMachinePreferenceObjectWrapper(reconciler VirtualMachinePreferenceReconciler) reconciling.ObjectReconciler {
return func(existing ctrlruntimeclient.Object) (ctrlruntimeclient.Object, error) {
if existing != nil {
return reconciler(existing.(*instancetypev1alpha1.VirtualMachinePreference))
}
return reconciler(&instancetypev1alpha1.VirtualMachinePreference{})
}
}

// ReconcileVirtualMachinePreferences will create and update the VirtualMachinePreferences coming from the passed VirtualMachinePreferenceReconciler slice.
func ReconcileVirtualMachinePreferences(ctx context.Context, namedFactories []NamedVirtualMachinePreferenceReconcilerFactory, namespace string, client ctrlruntimeclient.Client, objectModifiers ...reconciling.ObjectModifier) error {
for _, factory := range namedFactories {
name, reconciler := factory()
reconcileObject := VirtualMachinePreferenceObjectWrapper(reconciler)
reconcileObject = reconciling.CreateWithNamespace(reconcileObject, namespace)
reconcileObject = reconciling.CreateWithName(reconcileObject, name)

for _, objectModifier := range objectModifiers {
reconcileObject = objectModifier(reconcileObject)
}

if err := reconciling.EnsureNamedObject(ctx, types.NamespacedName{Namespace: namespace, Name: name}, reconcileObject, client, &instancetypev1alpha1.VirtualMachinePreference{}, false); err != nil {
return fmt.Errorf("failed to ensure VirtualMachinePreference %s/%s: %w", namespace, name, err)
}
}

return nil
}
4 changes: 4 additions & 0 deletions modules/api/pkg/test/e2e/utils/apiclient/models/a_k_s.go

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

4 changes: 4 additions & 0 deletions modules/api/pkg/test/e2e/utils/apiclient/models/a_w_s.go

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

4 changes: 4 additions & 0 deletions modules/api/pkg/test/e2e/utils/apiclient/models/alibaba.go

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

4 changes: 4 additions & 0 deletions modules/api/pkg/test/e2e/utils/apiclient/models/anexia.go

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

4 changes: 4 additions & 0 deletions modules/api/pkg/test/e2e/utils/apiclient/models/azure.go

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

4 changes: 4 additions & 0 deletions modules/api/pkg/test/e2e/utils/apiclient/models/baremetal.go

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

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

4 changes: 4 additions & 0 deletions modules/api/pkg/test/e2e/utils/apiclient/models/e_k_s.go

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

4 changes: 4 additions & 0 deletions modules/api/pkg/test/e2e/utils/apiclient/models/fake.go

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

4 changes: 4 additions & 0 deletions modules/api/pkg/test/e2e/utils/apiclient/models/g_c_p.go

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

4 changes: 4 additions & 0 deletions modules/api/pkg/test/e2e/utils/apiclient/models/g_k_e.go

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

4 changes: 4 additions & 0 deletions modules/api/pkg/test/e2e/utils/apiclient/models/hetzner.go

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

Loading

0 comments on commit 2f98897

Please sign in to comment.