Skip to content

Commit

Permalink
module deletion (kyma-project#101)
Browse files Browse the repository at this point in the history
* - add moduleInfo to kyma conditions to track current module information
- add logic to delete not exist modules

* - relocate moduleInfos to kyma.status

* - remove unnecessary condition reassignment

* - change kyma sample to sync mode

* - add FilterNotExistsConditions
- add new module for testing

* - add test case for module deleting

* - fix linter complains

* - renaming

* - reuse getModule

* - fix wrong condition
  • Loading branch information
ruanxin authored Aug 5, 2022
1 parent da0452f commit 41844fd
Show file tree
Hide file tree
Showing 18 changed files with 578 additions and 97 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ out.txt
*/__debug_bin

.run/
/operator/config/crd/bases/component.kyma-project.io_kcpmodules.yaml
/operator/config/crd/bases/component.kyma-project.io_skrmodules.yaml
/operator/config/crd/bases/component.kyma-project.io*
.test.env
**/**.code-workspace
9 changes: 9 additions & 0 deletions operator/.golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ linters-settings:
- standard # Captures all standard packages if they do not match another section.
- default # Contains all imports that could not be matched to another section type.
- prefix(github.com/kyma-project)
cyclop:
max-complexity: 15
ireturn:
allow:
- anon
- error
- empty
- stdlib
- Client
issues:
max-issues-per-linter: 0
max-same-issues: 0
Expand Down
96 changes: 92 additions & 4 deletions operator/api/v1alpha1/kyma_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ type KymaStatus struct {
// +optional
Conditions []KymaCondition `json:"conditions,omitempty"`

// Contains essential information about the current deployed module
ModuleInfos []ModuleInfo `json:"moduleInfos,omitempty"`

// Observed generation
// +optional
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
Expand Down Expand Up @@ -214,6 +217,23 @@ type KymaCondition struct {
LastTransitionTime *metav1.Time `json:"lastTransitionTime,omitempty"`
}

type ModuleInfo struct {
// Name is the current deployed module name
Name string `json:"name"`

// ModuleName is the unique identifier of the module.
ModuleName string `json:"moduleName"`

// Channel is the current deployed module channel
Channel Channel `json:"channel"`

// GroupVersionKind is the current deployed module gvk
GroupVersionKind metav1.GroupVersionKind `json:"gvk"`

// Namespace is the current deployed module namespace
Namespace string `json:"namespace"`
}

type TemplateInfo struct {
// Generation tracks the active Generation of the ModuleTemplate. In Case it changes, the new Generation will differ
// from the one tracked in TemplateInfo and thus trigger a new reconciliation with a newly parser ModuleTemplate
Expand Down Expand Up @@ -289,6 +309,44 @@ func (kyma *Kyma) SetLastSync() *Kyma {
return kyma
}

type moduleInfoExistsPair struct {
moduleInfo *ModuleInfo
exists bool
}

func (kyma *Kyma) GetNoLongerExistingModuleInfos() []*ModuleInfo {
moduleInfoMap := make(map[string]*moduleInfoExistsPair)

for i := range kyma.Status.ModuleInfos {
moduleInfo := &kyma.Status.ModuleInfos[i]
moduleInfoMap[moduleInfo.ModuleName] = &moduleInfoExistsPair{exists: false, moduleInfo: moduleInfo}
}

for i := range kyma.Spec.Modules {
module := &kyma.Spec.Modules[i]
if _, exists := moduleInfoMap[module.Name]; exists {
moduleInfoMap[module.Name].exists = true
}
}

notExistsModules := make([]*ModuleInfo, 0)
for _, item := range moduleInfoMap {
if !item.exists {
notExistsModules = append(notExistsModules, item.moduleInfo)
}
}
return notExistsModules
}

func (kyma *Kyma) GetModuleInfoMap() map[string]*ModuleInfo {
moduleInfoMap := make(map[string]*ModuleInfo)
for i := range kyma.Status.ModuleInfos {
moduleInfo := &kyma.Status.ModuleInfos[i]
moduleInfoMap[moduleInfo.ModuleName] = moduleInfo
}
return moduleInfoMap
}

//+kubebuilder:object:root=true

// KymaList contains a list of Kyma.
Expand All @@ -305,8 +363,7 @@ func init() {

const NewModuleMessage = "new module"

func (kyma *Kyma) MatchConditionsToModules() []KymaCondition {
newConditions := []KymaCondition{}
func (kyma *Kyma) MatchConditionsToModules() {
for _, module := range kyma.Spec.Modules {
found := false
for _, condition := range kyma.Status.Conditions {
Expand All @@ -322,8 +379,39 @@ func (kyma *Kyma) MatchConditionsToModules() []KymaCondition {
Message: NewModuleMessage,
}
kyma.Status.Conditions = append(kyma.Status.Conditions, newCondition)
newConditions = append(kyma.Status.Conditions, newCondition)
}
}
return newConditions
}

type conditionExistsPair struct {
condition *KymaCondition
exists bool
}

// TODO: drop this after condition.Reason != module.Name.
func (kyma *Kyma) FilterNotExistsConditions() bool {
conditionsMap := make(map[string]*conditionExistsPair)
updateRequired := false
for i := range kyma.Status.Conditions {
condition := &kyma.Status.Conditions[i]
conditionsMap[condition.Reason] = &conditionExistsPair{exists: false, condition: condition}
}

for i := range kyma.Spec.Modules {
module := &kyma.Spec.Modules[i]
if _, exists := conditionsMap[module.Name]; exists {
conditionsMap[module.Name].exists = true
}
}

existsModules := make([]KymaCondition, 0)
for _, item := range conditionsMap {
if item.exists {
existsModules = append(existsModules, *item.condition)
} else {
updateRequired = true
}
}
kyma.Status.Conditions = existsModules
return updateRequired
}
21 changes: 21 additions & 0 deletions operator/api/v1alpha1/zz_generated.deepcopy.go

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

44 changes: 44 additions & 0 deletions operator/config/crd/bases/operator.kyma-project.io_kymas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,50 @@ spec:
- type
type: object
type: array
moduleInfos:
description: Contains essential information about the current deployed
module
items:
properties:
channel:
description: Channel is the current deployed module channel
enum:
- rapid
- regular
- stable
type: string
gvk:
description: GroupVersionKind is the current deployed module
gvk
properties:
group:
type: string
kind:
type: string
version:
type: string
required:
- group
- kind
- version
type: object
moduleName:
description: ModuleName is the unique identifier of the module.
type: string
name:
description: Name is the current deployed module name
type: string
namespace:
description: Namespace is the current deployed module namespace
type: string
required:
- channel
- gvk
- moduleName
- name
- namespace
type: object
type: array
observedGeneration:
description: Observed generation
format: int64
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.9.2
creationTimestamp: null
name: skrmodule2s.component.kyma-project.io
spec:
group: component.kyma-project.io
names:
kind: SKRModule2
listKind: SKRModule2List
plural: skrmodule2s
singular: skrmodule2
scope: Namespaced
versions:
- additionalPrinterColumns:
- jsonPath: .status.state
name: State
type: string
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
name: v1alpha1
schema:
openAPIV3Schema:
description: SKRModule2 is the Schema for the moduletemplates API.
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
description: SKRModuleSpec defines the desired state of SKRModule.
properties:
initKey:
type: string
type: object
status:
description: SKRModuleStatus defines the observed state of Manifest.
properties:
state:
enum:
- Processing
- Deleting
- Ready
- Error
type: string
type: object
type: object
served: true
storage: true
subresources: {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright 2022.
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.
*/

//+groupName=component.kyma-project.io
package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

//+genclient
//+kubebuilder:object:root=true
//+kubebuilder:storageversion
//+kubebuilder:printcolumn:name="State",type=string,JSONPath=".status.state"
//+kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
// SKRModule2 is the Schema for the moduletemplates API.
type SKRModule2 struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec SKRModuleSpec `json:"spec,omitempty"`
Status SKRModuleStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true

// SKRModule2List contains a list of SKRModule2.
type SKRModule2List struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []SKRModule2 `json:"items"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ spec:
enabled: true
modules:
- name: skr-module
controller: manifest
- name: skr-module2
- name: kcp-module
controller: kcp-operator
Loading

0 comments on commit 41844fd

Please sign in to comment.