Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/Lanture1064/arcadia into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Lanture1064 committed Aug 18, 2023
2 parents 0046bc2 + 4e89f69 commit 45fe615
Show file tree
Hide file tree
Showing 31 changed files with 1,063 additions and 25 deletions.
3 changes: 3 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ resources:
kind: Prompt
path: github.com/kubeagi/arcadia/api/v1alpha1
version: v1alpha1
webhooks:
validation: true
webhookVersion: v1
version: "3"
7 changes: 0 additions & 7 deletions api/v1alpha1/llm.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ var (
ErrMissingAPIKey = errors.New("missing apikey in auth info")
)

type LLMType string

const (
OpenAI LLMType = "openai"
ZhiPuAI LLMType = "zhipuai"
)

func (o *AuthInfo) FromSecret(secret corev1.Secret) error {
o.APIKey = string(secret.Data["apiKey"])
if o.APIKey == "" {
Expand Down
5 changes: 3 additions & 2 deletions api/v1alpha1/llm_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ limitations under the License.
package v1alpha1

import (
"github.com/kubeagi/arcadia/pkg/llms"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// LLMSpec defines the desired state of LLM
type LLMSpec struct {
DisplayName string `json:"displayName,omitempty"`
// Type defines the type of llm
Type LLMType `json:"type"`
// URL keeps the URL of the llm service(required)
Type llms.LLMType `json:"type"`
// URL keeps the URL of the llm service(Must required)
URL string `json:"url"`
// Auth keeps the authentication credentials when access llm
// keeps in k8s secret
Expand Down
13 changes: 5 additions & 8 deletions api/v1alpha1/prompt_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,16 @@ limitations under the License.
package v1alpha1

import (
llmzhipuai "github.com/kubeagi/arcadia/pkg/llms/zhipuai"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// PromptSpec defines the desired state of Prompt
type PromptSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

// Foo is an example field of Prompt. Edit prompt_types.go to remove/update
Foo string `json:"foo,omitempty"`
// LLM serivice name(CRD LLM)
LLM string `json:"llm"`
// ZhiPuAIParams defines the params of ZhiPuAI
ZhiPuAIParams *llmzhipuai.ModelParams `json:"zhiPuAIParams,omitempty"`
}

// PromptStatus defines the observed state of Prompt
Expand Down
93 changes: 93 additions & 0 deletions api/v1alpha1/prompt_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
Copyright 2023 KubeAGI.
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 (
"context"

llmzhipuai "github.com/kubeagi/arcadia/pkg/llms/zhipuai"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

// log is for logging in this package.
var promptlog = logf.Log.WithName("prompt-resource")

func (p *Prompt) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(p).
WithDefaulter(p).
WithValidator(p).
Complete()
}

//+kubebuilder:webhook:path=/mutate-arcadia-kubeagi-k8s-com-cn-v1alpha1-prompt,mutating=true,failurePolicy=fail,sideEffects=None,groups=arcadia.kubeagi.k8s.com.cn,resources=portals,verbs=create;update,versions=v1alpha1,name=mprompt.kb.io,admissionReviewVersions=v1

var _ webhook.CustomDefaulter = &Prompt{}

func (p *Prompt) Default(ctx context.Context, obj runtime.Object) error {
promptlog.Info("default", "name", p.Name)

// Override p.Spec.ZhiPuAIParams with default values if not nil
if p.Spec.ZhiPuAIParams != nil {
merged := llmzhipuai.MergeParams(*p.Spec.ZhiPuAIParams, llmzhipuai.DefaultModelParams())
p.Spec.ZhiPuAIParams = &merged
}

return nil
}

// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
//+kubebuilder:webhook:path=/validate-arcadia-kubeagi-k8s-com-cn-v1alpha1-prompt,mutating=false,failurePolicy=fail,sideEffects=None,groups=arcadia.kubeagi.k8s.com.cn,resources=prompts,verbs=create;update;delete,versions=v1alpha1,name=vprompt.kb.io,admissionReviewVersions=v1

var _ webhook.CustomValidator = &Prompt{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *Prompt) ValidateCreate(ctx context.Context, obj runtime.Object) error {
promptlog.Info("validate create", "name", r.Name)

if r.Spec.ZhiPuAIParams != nil {
if err := llmzhipuai.ValidateModelParams(*r.Spec.ZhiPuAIParams); err != nil {
promptlog.Error(err, "validate model params")
return err
}
}

return nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *Prompt) ValidateUpdate(ctx context.Context, oldObj runtime.Object, newObj runtime.Object) error {
promptlog.Info("validate update", "name", r.Name)

if r.Spec.ZhiPuAIParams != nil {
if err := llmzhipuai.ValidateModelParams(*r.Spec.ZhiPuAIParams); err != nil {
promptlog.Error(err, "validate model params")
return err
}
}

return nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *Prompt) ValidateDelete(ctx context.Context, obj runtime.Object) error {
promptlog.Info("validate delete", "name", r.Name)
return nil
}
135 changes: 135 additions & 0 deletions api/v1alpha1/webhook_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
Copyright 2023 KubeAGI.
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 (
"context"
"crypto/tls"
"fmt"
"net"
"path/filepath"
"testing"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

admissionv1beta1 "k8s.io/api/admission/v1beta1"
//+kubebuilder:scaffold:imports
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment
var ctx context.Context
var cancel context.CancelFunc

func TestAPIs(t *testing.T) {
RegisterFailHandler(Fail)

RunSpecsWithDefaultAndCustomReporters(t,
"Webhook Suite",
[]Reporter{printer.NewlineReporter{}})
}

var _ = BeforeSuite(func() {
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

ctx, cancel = context.WithCancel(context.TODO())

By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
ErrorIfCRDPathMissing: false,
WebhookInstallOptions: envtest.WebhookInstallOptions{
Paths: []string{filepath.Join("..", "..", "config", "webhook")},
},
}

var err error
// cfg is defined in this file globally.
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

scheme := runtime.NewScheme()
err = AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

err = admissionv1beta1.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

//+kubebuilder:scaffold:scheme

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())

// start webhook server using Manager
webhookInstallOptions := &testEnv.WebhookInstallOptions
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme,
Host: webhookInstallOptions.LocalServingHost,
Port: webhookInstallOptions.LocalServingPort,
CertDir: webhookInstallOptions.LocalServingCertDir,
LeaderElection: false,
MetricsBindAddress: "0",
})
Expect(err).NotTo(HaveOccurred())

err = (&Prompt{}).SetupWebhookWithManager(mgr)
Expect(err).NotTo(HaveOccurred())

//+kubebuilder:scaffold:webhook

go func() {
defer GinkgoRecover()
err = mgr.Start(ctx)
Expect(err).NotTo(HaveOccurred())
}()

// wait for the webhook server to get ready
dialer := &net.Dialer{Timeout: time.Second}
addrPort := fmt.Sprintf("%s:%d", webhookInstallOptions.LocalServingHost, webhookInstallOptions.LocalServingPort)
Eventually(func() error {
conn, err := tls.DialWithDialer(dialer, "tcp", addrPort, &tls.Config{InsecureSkipVerify: true})
if err != nil {
return err
}
conn.Close()
return nil
}).Should(Succeed())

}, 60)

var _ = AfterSuite(func() {
cancel()
By("tearing down the test environment")
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
})
10 changes: 8 additions & 2 deletions api/v1alpha1/zz_generated.deepcopy.go

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

2 changes: 1 addition & 1 deletion assets/arch.drawio

Large diffs are not rendered by default.

Binary file modified assets/arch.drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions config/certmanager/certificate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# The following manifests contain a self-signed issuer CR and a certificate CR.
# More document can be found at https://docs.cert-manager.io
# WARNING: Targets CertManager v1.0. Check https://cert-manager.io/docs/installation/upgrading/ for breaking changes.
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
name: selfsigned-issuer
namespace: system
spec:
selfSigned: {}
---
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: serving-cert # this name should match the one appeared in kustomizeconfig.yaml
namespace: system
spec:
# $(SERVICE_NAME) and $(SERVICE_NAMESPACE) will be substituted by kustomize
dnsNames:
- $(SERVICE_NAME).$(SERVICE_NAMESPACE).svc
- $(SERVICE_NAME).$(SERVICE_NAMESPACE).svc.cluster.local
issuerRef:
kind: Issuer
name: selfsigned-issuer
secretName: webhook-server-cert # this secret will not be prefixed, since it's not managed by kustomize
5 changes: 5 additions & 0 deletions config/certmanager/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resources:
- certificate.yaml

configurations:
- kustomizeconfig.yaml
16 changes: 16 additions & 0 deletions config/certmanager/kustomizeconfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This configuration is for teaching kustomize how to update name ref and var substitution
nameReference:
- kind: Issuer
group: cert-manager.io
fieldSpecs:
- kind: Certificate
group: cert-manager.io
path: spec/issuerRef/name

varReference:
- kind: Certificate
group: cert-manager.io
path: spec/commonName
- kind: Certificate
group: cert-manager.io
path: spec/dnsNames
Loading

0 comments on commit 45fe615

Please sign in to comment.