-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* move storage-network-discoverer from sds-replicated-volume
Signed-off-by: Ivan.Makeev <[email protected]>
- Loading branch information
Showing
13 changed files
with
1,225 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
module storage-network-controller | ||
|
||
go 1.22.7 | ||
|
||
require ( | ||
github.com/go-logr/logr v1.4.2 | ||
github.com/square/exit v1.1.0 | ||
k8s.io/api v0.31.0 | ||
k8s.io/apimachinery v0.31.0 // indirect | ||
k8s.io/client-go v0.31.0 | ||
k8s.io/klog/v2 v2.130.1 | ||
sigs.k8s.io/controller-runtime v0.19.0 | ||
) | ||
|
||
require ( | ||
github.com/beorn7/perks v1.0.1 // indirect | ||
github.com/cespare/xxhash/v2 v2.3.0 // indirect | ||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect | ||
github.com/emicklei/go-restful/v3 v3.11.0 // indirect | ||
github.com/evanphx/json-patch/v5 v5.9.0 // indirect | ||
github.com/fsnotify/fsnotify v1.7.0 // indirect | ||
github.com/fxamacker/cbor/v2 v2.7.0 // indirect | ||
github.com/go-openapi/jsonpointer v0.19.6 // indirect | ||
github.com/go-openapi/jsonreference v0.20.2 // indirect | ||
github.com/go-openapi/swag v0.22.4 // indirect | ||
github.com/gogo/protobuf v1.3.2 // indirect | ||
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect | ||
github.com/golang/protobuf v1.5.4 // indirect | ||
github.com/google/gnostic-models v0.6.8 // indirect | ||
github.com/google/go-cmp v0.6.0 // indirect | ||
github.com/google/gofuzz v1.2.0 // indirect | ||
github.com/google/uuid v1.6.0 // indirect | ||
github.com/imdario/mergo v0.3.6 // indirect | ||
github.com/josharian/intern v1.0.0 // indirect | ||
github.com/json-iterator/go v1.1.12 // indirect | ||
github.com/mailru/easyjson v0.7.7 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect | ||
github.com/pkg/errors v0.9.1 // indirect | ||
github.com/prometheus/client_golang v1.19.1 // indirect | ||
github.com/prometheus/client_model v0.6.1 // indirect | ||
github.com/prometheus/common v0.55.0 // indirect | ||
github.com/prometheus/procfs v0.15.1 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
github.com/x448/float16 v0.8.4 // indirect | ||
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc // indirect | ||
golang.org/x/net v0.26.0 // indirect | ||
golang.org/x/oauth2 v0.21.0 // indirect | ||
golang.org/x/sys v0.21.0 // indirect | ||
golang.org/x/term v0.21.0 // indirect | ||
golang.org/x/text v0.16.0 // indirect | ||
golang.org/x/time v0.3.0 // indirect | ||
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect | ||
google.golang.org/protobuf v1.34.2 // indirect | ||
gopkg.in/inf.v0 v0.9.1 // indirect | ||
gopkg.in/yaml.v2 v2.4.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
k8s.io/apiextensions-apiserver v0.31.0 // indirect | ||
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect | ||
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect | ||
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect | ||
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect | ||
sigs.k8s.io/yaml v1.4.0 // indirect | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
132 changes: 132 additions & 0 deletions
132
images/storage-network-discoverer/src/internal/config/config.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
Copyright 2024 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 config | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"net" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
||
"storage-network-controller/internal/logger" | ||
) | ||
|
||
const ( | ||
ControllerNamespaceEnv = "CONTROLLER_NAMESPACE" | ||
DiscoverySecEnv = "DISCOVERY_INTERVAL_SEC" | ||
CacheSecEnv = "CACHE_TTL_SEC" | ||
DefaultDiscoverySec = 15 | ||
DefaultCacheTTLSec = 30 | ||
HardcodedControllerNS = "d8-sds-replicated-volume" | ||
LogLevelEnv = "LOG_LEVEL" | ||
) | ||
|
||
type StorageNetworkCIDR []string | ||
|
||
type Options struct { | ||
ControllerNamespace string | ||
DiscoverySec int | ||
CacheTTLSec int | ||
Loglevel logger.Verbosity | ||
StorageNetworkCIDR StorageNetworkCIDR | ||
} | ||
|
||
// String is an implementation of the flag.Value interface | ||
func (i *StorageNetworkCIDR) String() string { | ||
return strings.Join(*i, " ") | ||
} | ||
|
||
// Set is an implementation of the flag.Value interface | ||
func (i *StorageNetworkCIDR) Set(value string) error { | ||
ip, _, err := net.ParseCIDR(value) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if !ip.IsPrivate() { | ||
return fmt.Errorf("IP %s must be in private ranges", value) | ||
} | ||
|
||
*i = append(*i, value) | ||
return nil | ||
} | ||
|
||
func NewConfig() (*Options, error) { | ||
var opts Options | ||
|
||
loglevel := os.Getenv(LogLevelEnv) | ||
if loglevel == "" { | ||
opts.Loglevel = logger.DebugLevel | ||
} else { | ||
opts.Loglevel = logger.Verbosity(loglevel) | ||
} | ||
|
||
discoverySec := os.Getenv(DiscoverySecEnv) | ||
if discoverySec == "" { | ||
opts.DiscoverySec = DefaultDiscoverySec | ||
} else { | ||
i, err := strconv.Atoi(discoverySec) | ||
if err != nil { | ||
fmt.Printf("Failed to convert value of env var %s to integer: %s", DiscoverySecEnv, err.Error()) | ||
fmt.Printf("Using default %d seconds", DefaultDiscoverySec) | ||
opts.DiscoverySec = DefaultDiscoverySec | ||
} else { | ||
opts.DiscoverySec = i | ||
} | ||
} | ||
|
||
cacheTTLSec := os.Getenv(CacheSecEnv) | ||
if cacheTTLSec == "" { | ||
opts.CacheTTLSec = DefaultCacheTTLSec | ||
} else { | ||
i, err := strconv.Atoi(cacheTTLSec) | ||
if err != nil { | ||
fmt.Printf("Failed to convert value of env var %s to integer: %s", CacheSecEnv, err.Error()) | ||
fmt.Printf("Using default %d seconds", DefaultCacheTTLSec) | ||
opts.CacheTTLSec = DefaultCacheTTLSec | ||
} else { | ||
opts.CacheTTLSec = i | ||
} | ||
} | ||
|
||
opts.ControllerNamespace = os.Getenv(ControllerNamespaceEnv) | ||
if opts.ControllerNamespace == "" { | ||
namespace, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace") | ||
if err != nil { | ||
fmt.Printf("Failed to get namespace from filesystem: %s", err.Error()) | ||
fmt.Printf("Using hardcoded namespace: %s", HardcodedControllerNS) | ||
opts.ControllerNamespace = HardcodedControllerNS | ||
} else { | ||
fmt.Printf("Got namespace from filesystem: %s", string(namespace)) | ||
opts.ControllerNamespace = string(namespace) | ||
} | ||
} | ||
|
||
fl := flag.NewFlagSet(os.Args[0], flag.ExitOnError) | ||
fl.Var(&opts.StorageNetworkCIDR, "storage-network-cidr", "Set storage network CIDR blocks. Can be passed multiple times.") | ||
|
||
err := fl.Parse(os.Args[1:]) | ||
if err != nil { | ||
fmt.Printf("error parsing flags, err: %s\n", err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
return &opts, nil | ||
} |
103 changes: 103 additions & 0 deletions
103
images/storage-network-discoverer/src/internal/logger/logger.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
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 logger | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/go-logr/logr" | ||
"k8s.io/klog/v2/textlogger" | ||
) | ||
|
||
const ( | ||
ErrorLevel Verbosity = "0" | ||
WarningLevel Verbosity = "1" | ||
InfoLevel Verbosity = "2" | ||
DebugLevel Verbosity = "3" | ||
TraceLevel Verbosity = "4" | ||
) | ||
|
||
const ( | ||
warnLvl = iota + 1 | ||
infoLvl | ||
debugLvl | ||
traceLvl | ||
) | ||
|
||
type ( | ||
Verbosity string | ||
// struct just to be unique in context | ||
loggerKey struct{} | ||
) | ||
|
||
type Logger struct { | ||
log logr.Logger | ||
} | ||
|
||
func NewLogger(level Verbosity) (*Logger, error) { | ||
v, err := strconv.Atoi(string(level)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
log := textlogger.NewLogger(textlogger.NewConfig(textlogger.Verbosity(v))).WithCallDepth(1) | ||
|
||
return &Logger{log: log}, nil | ||
} | ||
|
||
func WithLogger(ctx context.Context, logger *Logger) context.Context { | ||
return context.WithValue(ctx, loggerKey{}, logger) | ||
} | ||
|
||
func FromContext(ctx context.Context) *Logger { | ||
if logger, ok := ctx.Value(loggerKey{}).(*Logger); ok { | ||
return logger | ||
} | ||
|
||
// WARNING! There will be a panic here if the logger was not initialized in the context | ||
return nil | ||
} | ||
|
||
func (l Logger) GetLogger() logr.Logger { | ||
return l.log | ||
} | ||
|
||
func (l Logger) Error(err error, message string, keysAndValues ...interface{}) { | ||
l.log.Error(err, fmt.Sprintf("ERROR %s", message), keysAndValues...) | ||
} | ||
|
||
func (l Logger) Warning(message string, keysAndValues ...interface{}) { | ||
l.log.V(warnLvl).Info(fmt.Sprintf("WARNING %s", message), keysAndValues...) | ||
} | ||
|
||
func (l Logger) Info(message string, keysAndValues ...interface{}) { | ||
l.log.V(infoLvl).Info(fmt.Sprintf("INFO %s", message), keysAndValues...) | ||
} | ||
|
||
func (l Logger) Debug(message string, keysAndValues ...interface{}) { | ||
l.log.V(debugLvl).Info(fmt.Sprintf("DEBUG %s", message), keysAndValues...) | ||
} | ||
|
||
func (l Logger) Trace(message string, keysAndValues ...interface{}) { | ||
l.log.V(traceLvl).Info(fmt.Sprintf("TRACE %s", message), keysAndValues...) | ||
} | ||
|
||
func (l *Logger) Printf(format string, args ...interface{}) { | ||
l.log.V(traceLvl).Info("%s", fmt.Sprintf(format, args...)) | ||
} |
41 changes: 41 additions & 0 deletions
41
images/storage-network-discoverer/src/internal/utils/utils.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
Copyright 2024 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 utils | ||
|
||
import ( | ||
"fmt" | ||
"net/netip" | ||
|
||
"storage-network-controller/internal/logger" | ||
) | ||
|
||
func IPInCIDR(networks []netip.Prefix, ip string, log *logger.Logger) bool { | ||
netIP, err := netip.ParseAddr(ip) | ||
|
||
if err != nil { | ||
log.Error(err, fmt.Sprintf("IPInCIDR: cannot parse IP %s", ip)) | ||
return false | ||
} | ||
|
||
for _, network := range networks { | ||
if network.Contains(netIP) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
Oops, something went wrong.