-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: init default controller config
Signed-off-by: Abirdcfly <[email protected]>
- Loading branch information
Showing
7 changed files
with
156 additions
and
16 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
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,17 @@ | ||
apiVersion: v1 | ||
data: | ||
config: | | ||
{ | ||
"systemDatasource": { | ||
"apiGroup": "arcadia.kubeagi.k8s.com.cn/v1alpha1", | ||
"kind": "Datasource", | ||
"name": "{{ .Release.Name }}-minio", | ||
"namespace": "{{ .Release.Namespace }}" | ||
} | ||
} | ||
kind: ConfigMap | ||
metadata: | ||
labels: | ||
control-plane: {{ .Release.Name }}-arcadia | ||
name: {{ .Release.Name }}-config | ||
namespace: {{ .Release.Namespace }} |
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
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
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
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,92 @@ | ||
package config | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
arcadiav1alpha1 "github.com/kubeagi/arcadia/api/v1alpha1" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/utils/env" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
const ( | ||
EnvConfigKey = "DEFAULT_CONFIG" | ||
EnvConfigValue = "arcadia-config" | ||
EnvNamespaceKey = "POD_NAMESPACE" | ||
|
||
InClusterNamespacePath = "/var/run/secrets/kubernetes.io/serviceaccount/namespace" | ||
) | ||
|
||
var ( | ||
ErrNoConfigEnv = fmt.Errorf("env:%s is not found", EnvConfigKey) | ||
ErrNoConfig = fmt.Errorf("config in configmap is empty") | ||
ErrNoNamespaceEnv = fmt.Errorf("not in cluster and env:%s is not found", EnvNamespaceKey) | ||
) | ||
|
||
func GetSystemDatasource(ctx context.Context, c client.Client) (source *arcadiav1alpha1.Datasource, err error) { | ||
config, err := GetConfig(ctx, c) | ||
if err != nil { | ||
return nil, err | ||
} | ||
name := config.SystemDatasource.Name | ||
var namespace string | ||
if config.SystemDatasource.Namespace != nil { | ||
namespace = *config.SystemDatasource.Namespace | ||
} else { | ||
namespace, err = GetSelfNamespace() | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
if err = c.Get(context.Background(), client.ObjectKey{Name: name, Namespace: namespace}, source); err != nil { | ||
return nil, err | ||
} | ||
return source, err | ||
} | ||
|
||
func GetConfig(ctx context.Context, c client.Client) (config *Config, err error) { | ||
cmName := env.GetString(EnvConfigKey, EnvConfigValue) | ||
if cmName == "" { | ||
return nil, ErrNoConfigEnv | ||
} | ||
cmNamespace, err := GetSelfNamespace() | ||
if err != nil { | ||
return nil, err | ||
} | ||
cm := &corev1.ConfigMap{} | ||
if err = c.Get(ctx, client.ObjectKey{Name: cmName, Namespace: cmNamespace}, cm); err != nil { | ||
return nil, err | ||
} | ||
value, ok := cm.Data["config"] | ||
if !ok || len(value) == 0 { | ||
return nil, ErrNoConfig | ||
} | ||
if err = json.Unmarshal([]byte(value), &config); err != nil { | ||
return nil, err | ||
} | ||
return config, nil | ||
} | ||
|
||
func GetSelfNamespace() (string, error) { | ||
// Check whether the namespace file exists. | ||
// If not, we are not running in cluster so can't guess the namespace. | ||
if _, err := os.Stat(InClusterNamespacePath); os.IsNotExist(err) { | ||
operatorNamespace := os.Getenv(EnvNamespaceKey) | ||
if operatorNamespace == "" { | ||
return "", ErrNoNamespaceEnv | ||
} | ||
return operatorNamespace, nil | ||
} else if err != nil { | ||
return "", fmt.Errorf("error checking namespace file: %w", err) | ||
} | ||
|
||
// Load the namespace file and return its content | ||
namespace, err := os.ReadFile(InClusterNamespacePath) | ||
if err != nil { | ||
return "", fmt.Errorf("error reading namespace file: %w", err) | ||
} | ||
return string(namespace), nil | ||
} |
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,11 @@ | ||
package config | ||
|
||
import ( | ||
arcadiav1alpha1 "github.com/kubeagi/arcadia/api/v1alpha1" | ||
) | ||
|
||
// Config defines the configuration for the Arcadia controller | ||
type Config struct { | ||
// SystemDatasource specifies the built-in datasource for Arcadia to host data files and model files | ||
SystemDatasource arcadiav1alpha1.TypedObjectReference `json:"systemDatasource,omitempty"` | ||
} |