Skip to content

Commit

Permalink
[controller] Fix internal hook and reduce image size (#32)
Browse files Browse the repository at this point in the history
Signed-off-by: v.oleynikov <[email protected]>
  • Loading branch information
duckhawk authored Aug 22, 2024
1 parent d2d31e4 commit cb6fa9f
Show file tree
Hide file tree
Showing 10 changed files with 342 additions and 67 deletions.
23 changes: 1 addition & 22 deletions hooks/lib/module/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,4 @@ def get_https_mode(module_name: str, values: dict) -> str:
raise Exception("https mode is not defined")

def get_module_name() -> str:
module = ""
file_path = os.path.abspath(__file__)
external_modules_dir = os.getenv("EXTERNAL_MODULES_DIR")
for dir in os.getenv("MODULES_DIR").split(":"):
if dir.startswith(external_modules_dir):
dir = external_modules_dir
if file_path.startswith(dir):
module = re.sub(f"{dir}/?\d?\d?\d?-?", "", file_path, 1).split("/")[0]
# /deckhouse/external-modules/virtualization/mr/hooks/hook_name.py
# {-------------------------- file_path --------------------------}
# {------ MODULES_DIR ------}{---------- regexp result ----------}}
# virtualization/mr/hooks/hook_name.py
# {-module-name-}{---------------------}
# or
# /deckhouse/modules/900-virtualization/hooks/hook_name.py
# {---------------------- file_path ----------------------}
# {-- MODULES_DIR --}{---{-------- regexp result --------}}
# virtualization/hooks/hook_name.py
# {-module-name-}{-----------------}

break
return module
return "csi-nfs"
4 changes: 2 additions & 2 deletions images/controller/src/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"d8-controller/pkg/kubutils"
"d8-controller/pkg/logger"
"fmt"
"github.com/deckhouse/csi-nfs/api/v1alpha1"
cn "github.com/deckhouse/csi-nfs/api/v1alpha1"
"os"
goruntime "runtime"

Expand All @@ -41,7 +41,7 @@ import (

var (
resourcesSchemeFuncs = []func(*apiruntime.Scheme) error{
v1alpha1.AddToScheme,
cn.AddToScheme,
clientgoscheme.AddToScheme,
extv1.AddToScheme,
v1.AddToScheme,
Expand Down
114 changes: 114 additions & 0 deletions images/webhooks/src/api/module_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
Copyright 2023 Flant JSC
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 v1alpha1

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

var (
// ModuleConfigGVR GroupVersionResource
ModuleConfigGVR = schema.GroupVersionResource{
Group: SchemeGroupVersion.Group,
Version: SchemeGroupVersion.Version,
Resource: "moduleconfigs",
}
ModuleConfigGVK = schema.GroupVersionKind{
Group: SchemeGroupVersion.Group,
Version: SchemeGroupVersion.Version,
Kind: "ModuleConfig",
}
)

var _ runtime.Object = (*ModuleConfig)(nil)

// +genclient
// +genclient:nonNamespaced
// +k8s:deepcopy-gen=true
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// ModuleConfig is a configuration for module or for global config values.
type ModuleConfig struct {
metav1.TypeMeta `json:",inline"`
// Standard object's metadata.
// More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
// +optional
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec ModuleConfigSpec `json:"spec"`

Status ModuleConfigStatus `json:"status,omitempty"`
}

// SettingsValues empty interface in needed to handle DeepCopy generation. DeepCopy does not work with unnamed empty interfaces
type SettingsValues map[string]interface{}

func (v *SettingsValues) DeepCopy() *SettingsValues {
nmap := make(map[string]interface{}, len(*v))

for key, value := range *v {
nmap[key] = value
}

vv := SettingsValues(nmap)

return &vv
}

func (v SettingsValues) DeepCopyInto(out *SettingsValues) {
{
v := &v
clone := v.DeepCopy()
*out = *clone
return
}
}

type ModuleConfigSpec struct {
Version int `json:"version,omitempty"`
Settings SettingsValues `json:"settings,omitempty"`
Enabled *bool `json:"enabled,omitempty"`
}

type ModuleConfigStatus struct {
Version string `json:"version"`
Message string `json:"message"`
}

// +k8s:deepcopy-gen=true
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// ModuleConfigList is a list of ModuleConfig resources
type ModuleConfigList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata"`

Items []ModuleConfig `json:"items"`
}

type moduleConfigKind struct{}

func (in *ModuleConfigStatus) GetObjectKind() schema.ObjectKind {
return &moduleConfigKind{}
}

func (f *moduleConfigKind) SetGroupVersionKind(_ schema.GroupVersionKind) {}
func (f *moduleConfigKind) GroupVersionKind() schema.GroupVersionKind {
return ModuleConfigGVK
}
54 changes: 54 additions & 0 deletions images/webhooks/src/api/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2021 Flant JSC
//
// 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 v1alpha1

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

// SchemeGroupVersion is group version used to register these objects
var SchemeGroupVersion = schema.GroupVersion{Group: "deckhouse.io", Version: "v1alpha1"}

// Resource takes an unqualified resource and returns a Group qualified GroupResource
func Resource(resource string) schema.GroupResource {
return SchemeGroupVersion.WithResource(resource).GroupResource()
}

var (
// localSchemeBuilder and AddToScheme will stay in k8s.io/kubernetes.
SchemeBuilder runtime.SchemeBuilder
localSchemeBuilder = &SchemeBuilder
AddToScheme = localSchemeBuilder.AddToScheme
)

func init() {
// We only register manually written functions here. The registration of the
// generated functions takes place in the generated files. The separation
// makes the code compile even when the generated files are missing.
localSchemeBuilder.Register(addKnownTypes)
}

// Adds the list of known types to api.Scheme.
func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(SchemeGroupVersion,
&ModuleConfig{},
&ModuleConfigList{},
)

metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
return nil
}
125 changes: 125 additions & 0 deletions images/webhooks/src/api/zz_generated.deepcopy.go

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

33 changes: 33 additions & 0 deletions images/webhooks/src/api/zz_generated.defaults.go

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

Loading

0 comments on commit cb6fa9f

Please sign in to comment.