Skip to content

Commit

Permalink
chore: add settings related labels and annotations (#247)
Browse files Browse the repository at this point in the history
* chore: add settings related labels and annotations
* chore: add some methods used by command

Co-authored-by: fm <[email protected]>
  • Loading branch information
nanjingfm and fm authored Aug 19, 2022
1 parent 5600685 commit 98126ec
Show file tree
Hide file tree
Showing 21 changed files with 667 additions and 4 deletions.
8 changes: 8 additions & 0 deletions apis/meta/v1alpha1/labels_annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ const (

// SecretSyncMutationLabelKey label key to select the suitable secret
SecretSyncMutationLabelKey = "integrations.katanomi.dev/integration.mutation"

// SettingsTypeLabelKey label key to select the settings secret
SettingsTypeLabelKey = "settings.katanomi.dev/settingsType"
)

// Common Annotations
Expand All @@ -72,6 +75,10 @@ const (
ClusterNameAnnotationKey = "integrations.katanomi.dev/clusterName"
// TriggerNameAnnotationKey annotation key to store a friendly trigger name
TriggerNameAnnotationKey = "katanomi.dev/triggerName"
// SettingsConvertTypesKey annotation key to store the setting types need to be converted
SettingsConvertTypesKey = "settings.katanomi.dev/convertTypes"
// SettingsAutoGenerateKey annotation key to store whether the secret is automatically generated
SettingsAutoGenerateKey = "settings.katanomi.dev/autoGenerate"
)

// Attribute keys for Integrations
Expand All @@ -80,6 +87,7 @@ const (
AuthAttributeKey = "auth"
ReplicationPolicyTypesAttributeKey = "replicationPolicyTypes"
ResourceTypesAttributeKey = "resourceTypes"
SettingsTypesAttributeKey = "settingsTypes"
MethodsAttributeKey = "methods"
AllowEmptySecretAttributeKey = "allowEmptySecret"
DefaultProjectTypeAttributeKey = "defaultProjectSubType"
Expand Down
6 changes: 3 additions & 3 deletions apis/meta/v1alpha1/labels_annotations_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// CopyLabels from the left side object to the righ side
// CopyLabels from the left side object to the right side
// will override any existing labels
func CopyLabels(object, dest metav1.Object) {
labels := dest.GetLabels()
Expand All @@ -29,7 +29,7 @@ func CopyLabels(object, dest metav1.Object) {
dest.SetLabels(labels)
}

// CopyAnnotations from the left side object to the righ side
// CopyAnnotations from the left side object to the right side
// will override any existing values
func CopyAnnotations(object, dest metav1.Object) {
anno := dest.GetAnnotations()
Expand All @@ -38,7 +38,7 @@ func CopyAnnotations(object, dest metav1.Object) {
dest.SetAnnotations(anno)
}

// CopyMapStringString copies content from a map to annother
// CopyMapStringString copies content from a map to another
func CopyMapStringString(object, dest map[string]string) map[string]string {
if object != nil {
if dest == nil {
Expand Down
32 changes: 32 additions & 0 deletions command/io/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright 2022 The Katanomi Authors.
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 io

// Config configuration for copy operation
type Config struct {
FileFilter []func(srcFile, dstFile string) bool
}

// CopyOption function for setting CopyConfig
type CopyOption func(config *Config)

// FileFilterOption construct file filter `CopyOption`
func FileFilterOption(f func(srcFile, dstFile string) bool) CopyOption {
return func(config *Config) {
config.FileFilter = append(config.FileFilter, f)
}
}
121 changes: 121 additions & 0 deletions command/io/copy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
Copyright 2022 The Katanomi Authors.
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 io

import (
"io"
"io/ioutil"
"os"
"path"
)

// copyFile copies a files from src to dst.
func copyFile(src, dst string, srcInfo os.FileInfo, config *Config) error {
for _, filter := range config.FileFilter {
if filter(src, dst) {
return nil
}
}
s, err := os.Open(src)
if err != nil {
return err
}
defer s.Close()

if IsDir(dst) {
dst = path.Join(dst, path.Base(src))
} else {
if err = os.MkdirAll(path.Dir(dst), os.ModePerm); err != nil {
return err
}
}

d, err := os.OpenFile(dst, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, srcInfo.Mode()&os.ModePerm)
if err != nil {
return err
}
defer d.Close()

_, err = io.Copy(d, s)
return err
}

// copyDir copies a dir from src to dst
func copyDir(src, dst string, srcInfo os.FileInfo, config *Config) error {
var err error
var fds []os.FileInfo

if err = os.MkdirAll(dst, srcInfo.Mode()&os.ModePerm); err != nil {
return err
}

if fds, err = ioutil.ReadDir(src); err != nil {
return err
}
for _, fd := range fds {
srcFp := path.Join(src, fd.Name())
dstFp := path.Join(dst, fd.Name())

if err = switchboard(srcFp, dstFp, fd, config); err != nil {
return err
}
}
return nil
}

// Copy support copy file to file or dir to dir
func Copy(src, dst string, opts ...CopyOption) error {
config := &Config{}
for _, option := range opts {
option(config)
}
info, err := os.Lstat(src)
if err != nil {
return err
}
return switchboard(src, dst, info, config)
}

func copyLink(src, dest string, config *Config) (err error) {
var orig string
orig, err = os.Readlink(src)
if err != nil {
return err
}
if !path.IsAbs(orig) {
orig = path.Join(path.Dir(src), orig)
}
var info os.FileInfo
info, err = os.Lstat(orig)
if err != nil {
return err
}
return switchboard(orig, dest, info, config)
}

func switchboard(src, dest string, info os.FileInfo, config *Config) (err error) {
switch {
case info.Mode()&os.ModeSymlink != 0:
err = copyLink(src, dest, config)
case info.IsDir():
err = copyDir(src, dest, info, config)
default:
err = copyFile(src, dest, info, config)
}

return err
}
89 changes: 89 additions & 0 deletions command/io/copy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Copyright 2022 The Katanomi Authors.
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 io

import (
"bytes"
"io/ioutil"
"os"
"path"
"strings"
"testing"

. "github.com/onsi/gomega"
)

func TestCopy(t *testing.T) {
g := NewGomegaWithT(t)

cleanup := func() {
err := os.RemoveAll("./testdata/dest")
g.Expect(err).Should(Succeed())

err = os.MkdirAll("./testdata/dest", 0777)
g.Expect(err).Should(Succeed())
}

checkContent := func(file string, expectContent string) {
fileContent, err := ioutil.ReadFile(file)
g.Expect(err).Should(Succeed())
g.Expect(bytes.Contains(fileContent, []byte(expectContent))).To(BeTrue())
}

checkDstFileContent := func(fileName string) {
dstFile := path.Join("./testdata/dest", fileName)
g.Expect(IsFile(dstFile)).To(BeTrue())
checkContent(dstFile, "hello")
}

cleanup()
defer cleanup()

// copy dir to dir
err := Copy("./testdata/src", "./testdata/dest")
g.Expect(err).Should(Succeed())
checkDstFileContent("source-file")
checkDstFileContent("..source-file-link1")
checkDstFileContent("..source-file-link2")
checkDstFileContent("..source-file-link3")

// copy file to file
dstFile4 := path.Join("./testdata/dest", "dst-file-1")
err = Copy("./testdata/src/source-file", dstFile4)
g.Expect(err).Should(Succeed())
checkDstFileContent("dst-file-1")

dstFile5 := path.Join("./testdata/dest", "..dst-file-2")
err = Copy("./testdata/src/source-file", dstFile5, FileFilterOption(func(srcFile, dstFile string) bool {
if strings.HasPrefix(path.Base(dstFile), "..") {
return true
}
return false
}))
g.Expect(err).Should(Succeed())
g.Expect(IsExist(dstFile5)).To(BeFalse())

dstFile6 := path.Join("./testdata/dest", "..dst-file-3")
err = Copy("./testdata/src/source-file", dstFile6, FileFilterOption(func(srcFile, dstFile string) bool {
if strings.HasPrefix(path.Base(dstFile), "xx") {
return true
}
return false
}))
g.Expect(err).Should(Succeed())
checkDstFileContent("..dst-file-3")
}
18 changes: 18 additions & 0 deletions command/io/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Copyright 2022 The Katanomi Authors.
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 io some io operation functions
package io
62 changes: 62 additions & 0 deletions command/io/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright 2022 The Katanomi Authors.
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 io

import (
"os"
"path"
)

// WriteFile write the content to the desired file
func WriteFile(dstFile string, content []byte, perm os.FileMode) error {
if err := os.MkdirAll(path.Dir(dstFile), os.ModePerm); err != nil {
return err
}
f, err := os.OpenFile(dstFile, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, perm&os.ModePerm)
if err != nil {
return err
}
defer f.Close()

_, err = f.Write(content)
return err
}

// IsFile check if the given path is a file
func IsFile(file string) bool {
s, err := os.Stat(file)
if err != nil {
return false
}
return !s.IsDir()
}

// IsExist check if the given path is existed
func IsExist(path string) bool {
_, err := os.Stat(path)
return err == nil
}

// IsDir check if the given path is a dir
func IsDir(dir string) bool {
s, err := os.Stat(dir)
if err != nil {
return false
}

return s.IsDir()
}
Loading

0 comments on commit 98126ec

Please sign in to comment.