generated from crossplane/provider-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0777a16
commit 20c384a
Showing
6 changed files
with
434 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,18 @@ | ||
/* | ||
Copyright 2022 The Crossplane 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 key contains group key API versions | ||
package key |
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 @@ | ||
/* | ||
Copyright 2022 The Crossplane 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 v1alpha1 |
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,40 @@ | ||
/* | ||
Copyright 2020 The Crossplane 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 v1alpha1 contains the v1alpha1 group Sample resources of the Litellm provider. | ||
// +kubebuilder:object:generate=true | ||
// +groupName=key.litellm.crossplane.io | ||
// +versionName=v1alpha1 | ||
package v1alpha1 | ||
|
||
import ( | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"sigs.k8s.io/controller-runtime/pkg/scheme" | ||
) | ||
|
||
// Package type metadata. | ||
const ( | ||
Group = "key.litellm.crossplane.io" | ||
Version = "v1alpha1" | ||
) | ||
|
||
var ( | ||
// SchemeGroupVersion is group version used to register these objects | ||
SchemeGroupVersion = schema.GroupVersion{Group: Group, Version: Version} | ||
|
||
// SchemeBuilder is used to add go types to the GroupVersionKind scheme | ||
SchemeBuilder = &scheme.Builder{GroupVersion: SchemeGroupVersion} | ||
) |
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,86 @@ | ||
/* | ||
Copyright 2022 The Crossplane 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 v1alpha1 | ||
|
||
import ( | ||
"reflect" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
|
||
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" | ||
) | ||
|
||
// KeyParameters are the configurable fields of a Key. | ||
type KeyParameters struct { | ||
ConfigurableField string `json:"configurableField"` | ||
} | ||
|
||
// KeyObservation are the observable fields of a Key. | ||
type KeyObservation struct { | ||
ObservableField string `json:"observableField,omitempty"` | ||
} | ||
|
||
// A KeySpec defines the desired state of a Key. | ||
type KeySpec struct { | ||
xpv1.ResourceSpec `json:",inline"` | ||
ForProvider KeyParameters `json:"forProvider"` | ||
} | ||
|
||
// A KeyStatus represents the observed state of a Key. | ||
type KeyStatus struct { | ||
xpv1.ResourceStatus `json:",inline"` | ||
AtProvider KeyObservation `json:"atProvider,omitempty"` | ||
} | ||
|
||
// +kubebuilder:object:root=true | ||
|
||
// A Key is an example API type. | ||
// +kubebuilder:printcolumn:name="READY",type="string",JSONPath=".status.conditions[?(@.type=='Ready')].status" | ||
// +kubebuilder:printcolumn:name="SYNCED",type="string",JSONPath=".status.conditions[?(@.type=='Synced')].status" | ||
// +kubebuilder:printcolumn:name="EXTERNAL-NAME",type="string",JSONPath=".metadata.annotations.crossplane\\.io/external-name" | ||
// +kubebuilder:printcolumn:name="AGE",type="date",JSONPath=".metadata.creationTimestamp" | ||
// +kubebuilder:subresource:status | ||
// +kubebuilder:resource:scope=Cluster,categories={crossplane,managed,litellm} | ||
type Key struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ObjectMeta `json:"metadata,omitempty"` | ||
|
||
Spec KeySpec `json:"spec"` | ||
Status KeyStatus `json:"status,omitempty"` | ||
} | ||
|
||
// +kubebuilder:object:root=true | ||
|
||
// KeyList contains a list of Key | ||
type KeyList struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ListMeta `json:"metadata,omitempty"` | ||
Items []Key `json:"items"` | ||
} | ||
|
||
// Key type metadata. | ||
var ( | ||
KeyKind = reflect.TypeOf(Key{}).Name() | ||
KeyGroupKind = schema.GroupKind{Group: Group, Kind: KeyKind}.String() | ||
KeyKindAPIVersion = KeyKind + "." + SchemeGroupVersion.String() | ||
KeyGroupVersionKind = SchemeGroupVersion.WithKind(KeyKind) | ||
) | ||
|
||
func init() { | ||
SchemeBuilder.Register(&Key{}, &KeyList{}) | ||
} |
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,199 @@ | ||
/* | ||
Copyright 2022 The Crossplane 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 key | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
"k8s.io/apimachinery/pkg/types" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/crossplane/crossplane-runtime/pkg/connection" | ||
"github.com/crossplane/crossplane-runtime/pkg/controller" | ||
"github.com/crossplane/crossplane-runtime/pkg/event" | ||
"github.com/crossplane/crossplane-runtime/pkg/ratelimiter" | ||
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" | ||
"github.com/crossplane/crossplane-runtime/pkg/resource" | ||
|
||
"github.com/crossplane/provider-litellm/apis/key/v1alpha1" | ||
apisv1alpha1 "github.com/crossplane/provider-litellm/apis/v1alpha1" | ||
"github.com/crossplane/provider-litellm/internal/features" | ||
) | ||
|
||
const ( | ||
errNotKey = "managed resource is not a Key custom resource" | ||
errTrackPCUsage = "cannot track ProviderConfig usage" | ||
errGetPC = "cannot get ProviderConfig" | ||
errGetCreds = "cannot get credentials" | ||
|
||
errNewClient = "cannot create new Service" | ||
) | ||
|
||
// A NoOpService does nothing. | ||
type NoOpService struct{} | ||
|
||
var ( | ||
newNoOpService = func(_ []byte) (interface{}, error) { return &NoOpService{}, nil } | ||
) | ||
|
||
// Setup adds a controller that reconciles Key managed resources. | ||
func Setup(mgr ctrl.Manager, o controller.Options) error { | ||
name := managed.ControllerName(v1alpha1.KeyGroupKind) | ||
|
||
cps := []managed.ConnectionPublisher{managed.NewAPISecretPublisher(mgr.GetClient(), mgr.GetScheme())} | ||
if o.Features.Enabled(features.EnableAlphaExternalSecretStores) { | ||
cps = append(cps, connection.NewDetailsManager(mgr.GetClient(), apisv1alpha1.StoreConfigGroupVersionKind)) | ||
} | ||
|
||
r := managed.NewReconciler(mgr, | ||
resource.ManagedKind(v1alpha1.KeyGroupVersionKind), | ||
managed.WithExternalConnecter(&connector{ | ||
kube: mgr.GetClient(), | ||
usage: resource.NewProviderConfigUsageTracker(mgr.GetClient(), &apisv1alpha1.ProviderConfigUsage{}), | ||
newServiceFn: newNoOpService}), | ||
managed.WithLogger(o.Logger.WithValues("controller", name)), | ||
managed.WithPollInterval(o.PollInterval), | ||
managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), | ||
managed.WithConnectionPublishers(cps...)) | ||
|
||
return ctrl.NewControllerManagedBy(mgr). | ||
Named(name). | ||
WithOptions(o.ForControllerRuntime()). | ||
WithEventFilter(resource.DesiredStateChanged()). | ||
For(&v1alpha1.Key{}). | ||
Complete(ratelimiter.NewReconciler(name, r, o.GlobalRateLimiter)) | ||
} | ||
|
||
// A connector is expected to produce an ExternalClient when its Connect method | ||
// is called. | ||
type connector struct { | ||
kube client.Client | ||
usage resource.Tracker | ||
newServiceFn func(creds []byte) (interface{}, error) | ||
} | ||
|
||
// Connect typically produces an ExternalClient by: | ||
// 1. Tracking that the managed resource is using a ProviderConfig. | ||
// 2. Getting the managed resource's ProviderConfig. | ||
// 3. Getting the credentials specified by the ProviderConfig. | ||
// 4. Using the credentials to form a client. | ||
func (c *connector) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) { | ||
cr, ok := mg.(*v1alpha1.Key) | ||
if !ok { | ||
return nil, errors.New(errNotKey) | ||
} | ||
|
||
if err := c.usage.Track(ctx, mg); err != nil { | ||
return nil, errors.Wrap(err, errTrackPCUsage) | ||
} | ||
|
||
pc := &apisv1alpha1.ProviderConfig{} | ||
if err := c.kube.Get(ctx, types.NamespacedName{Name: cr.GetProviderConfigReference().Name}, pc); err != nil { | ||
return nil, errors.Wrap(err, errGetPC) | ||
} | ||
|
||
cd := pc.Spec.Credentials | ||
data, err := resource.CommonCredentialExtractor(ctx, cd.Source, c.kube, cd.CommonCredentialSelectors) | ||
if err != nil { | ||
return nil, errors.Wrap(err, errGetCreds) | ||
} | ||
|
||
svc, err := c.newServiceFn(data) | ||
if err != nil { | ||
return nil, errors.Wrap(err, errNewClient) | ||
} | ||
|
||
return &external{service: svc}, nil | ||
} | ||
|
||
// An ExternalClient observes, then either creates, updates, or deletes an | ||
// external resource to ensure it reflects the managed resource's desired state. | ||
type external struct { | ||
// A 'client' used to connect to the external resource API. In practice this | ||
// would be something like an AWS SDK client. | ||
service interface{} | ||
} | ||
|
||
func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.ExternalObservation, error) { | ||
cr, ok := mg.(*v1alpha1.Key) | ||
if !ok { | ||
return managed.ExternalObservation{}, errors.New(errNotKey) | ||
} | ||
|
||
// These fmt statements should be removed in the real implementation. | ||
fmt.Printf("Observing: %+v", cr) | ||
|
||
return managed.ExternalObservation{ | ||
// Return false when the external resource does not exist. This lets | ||
// the managed resource reconciler know that it needs to call Create to | ||
// (re)create the resource, or that it has successfully been deleted. | ||
ResourceExists: true, | ||
|
||
// Return false when the external resource exists, but it not up to date | ||
// with the desired managed resource state. This lets the managed | ||
// resource reconciler know that it needs to call Update. | ||
ResourceUpToDate: true, | ||
|
||
// Return any details that may be required to connect to the external | ||
// resource. These will be stored as the connection secret. | ||
ConnectionDetails: managed.ConnectionDetails{}, | ||
}, nil | ||
} | ||
|
||
func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.ExternalCreation, error) { | ||
cr, ok := mg.(*v1alpha1.Key) | ||
if !ok { | ||
return managed.ExternalCreation{}, errors.New(errNotKey) | ||
} | ||
|
||
fmt.Printf("Creating: %+v", cr) | ||
|
||
return managed.ExternalCreation{ | ||
// Optionally return any details that may be required to connect to the | ||
// external resource. These will be stored as the connection secret. | ||
ConnectionDetails: managed.ConnectionDetails{}, | ||
}, nil | ||
} | ||
|
||
func (c *external) Update(ctx context.Context, mg resource.Managed) (managed.ExternalUpdate, error) { | ||
cr, ok := mg.(*v1alpha1.Key) | ||
if !ok { | ||
return managed.ExternalUpdate{}, errors.New(errNotKey) | ||
} | ||
|
||
fmt.Printf("Updating: %+v", cr) | ||
|
||
return managed.ExternalUpdate{ | ||
// Optionally return any details that may be required to connect to the | ||
// external resource. These will be stored as the connection secret. | ||
ConnectionDetails: managed.ConnectionDetails{}, | ||
}, nil | ||
} | ||
|
||
func (c *external) Delete(ctx context.Context, mg resource.Managed) error { | ||
cr, ok := mg.(*v1alpha1.Key) | ||
if !ok { | ||
return errors.New(errNotKey) | ||
} | ||
|
||
fmt.Printf("Deleting: %+v", cr) | ||
|
||
return nil | ||
} |
Oops, something went wrong.