Skip to content

Commit

Permalink
feat: init default controller config
Browse files Browse the repository at this point in the history
Signed-off-by: Abirdcfly <[email protected]>
  • Loading branch information
Abirdcfly committed Nov 1, 2023
1 parent 39c4065 commit 533d317
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 16 deletions.
2 changes: 1 addition & 1 deletion charts/arcadia/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v2
name: arcadia
description: A Helm chart(KubeBB Component) for KubeAGI Arcadia
type: application
version: 0.1.11
version: 0.1.12
appVersion: "0.0.1"

keywords:
Expand Down
17 changes: 17 additions & 0 deletions charts/arcadia/templates/config.yaml
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 }}
13 changes: 13 additions & 0 deletions charts/arcadia/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ spec:
containers:
- command:
- /manager
env:
- name: POD_NAME
valueFrom:
fieldRef:
apiVersion: v1
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
apiVersion: v1
fieldPath: metadata.namespace
- name: DEFAULT_CONFIG
value: {{ .Release.Name }}-config
image: {{ .Values.deployment.image }}
imagePullPolicy: {{ .Values.deployment.imagePullPolicy }}
name: manager
Expand Down
30 changes: 22 additions & 8 deletions controllers/datasource_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"reflect"

"github.com/go-logr/logr"
"github.com/kubeagi/arcadia/pkg/config"

Check failure on line 25 in controllers/datasource_controller.go

View workflow job for this annotation

GitHub Actions / Lint Go code

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/kubeagi/arcadia) -s blank -s dot --custom-order (gci)
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -152,17 +153,30 @@ func (r *DatasourceReconciler) Checkdatasource(ctx context.Context, logger logr.
var info any
switch instance.Spec.Type() {
case arcadiav1alpha1.DatasourceTypeLocal:
// FIXME: implement local datasource check when system datasource defined by https://github.com/kubeagi/arcadia/issues/156
// 1. read system datasource endpoint
// 2. check against pre-denfined rules for local datasource rules
return r.UpdateStatus(ctx, instance, nil)
var endpoint *arcadiav1alpha1.Endpoint
if instance.Spec.Enpoint == nil {
system, err := config.GetSystemDatasource(ctx, r.Client)
if err != nil {
return r.UpdateStatus(ctx, instance, err)
}
endpoint = system.Spec.Enpoint.DeepCopy()
info = system.Spec.OSS.DeepCopy()
}
// set auth secret's namespace to the datasource's namespace
if endpoint != nil && endpoint.AuthSecret != nil {
endpoint.AuthSecret.WithNameSpace(instance.Namespace)
}
ds, err = datasource.NewLocal(ctx, r.Client, endpoint)
if err != nil {
return r.UpdateStatus(ctx, instance, err)
}
case arcadiav1alpha1.DatasourceTypeOSS:
endpoiont := instance.Spec.Enpoint.DeepCopy()
endpoint := instance.Spec.Enpoint.DeepCopy()
// set auth secret's namespace to the datasource's namespace
if endpoiont.AuthSecret != nil {
endpoiont.AuthSecret.WithNameSpace(instance.Namespace)
if endpoint.AuthSecret != nil {
endpoint.AuthSecret.WithNameSpace(instance.Namespace)
}
ds, err = datasource.NewOSS(ctx, r.Client, endpoiont)
ds, err = datasource.NewOSS(ctx, r.Client, endpoint)
if err != nil {
return r.UpdateStatus(ctx, instance, err)
}
Expand Down
7 changes: 0 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,3 @@ func main() {
os.Exit(1)
}
}

// TODO: Defines the configuration for the Arcadia controller and implement the config parser
// 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
}
92 changes: 92 additions & 0 deletions pkg/config/config.go
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"

Check failure on line 9 in pkg/config/config.go

View workflow job for this annotation

GitHub Actions / Lint Go code

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/kubeagi/arcadia) -s blank -s dot --custom-order (gci)
corev1 "k8s.io/api/core/v1"
"k8s.io/utils/env"
"sigs.k8s.io/controller-runtime/pkg/client"

Check failure on line 12 in pkg/config/config.go

View workflow job for this annotation

GitHub Actions / Lint Go code

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/kubeagi/arcadia) -s blank -s dot --custom-order (gci)
)

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
}
11 changes: 11 additions & 0 deletions pkg/config/config_type.go
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"`
}

0 comments on commit 533d317

Please sign in to comment.