Skip to content

Commit

Permalink
fix: Rename config -> values and release -> chart
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbroks committed Aug 22, 2023
1 parent dba6de7 commit 519cd1b
Show file tree
Hide file tree
Showing 17 changed files with 93 additions and 250 deletions.
4 changes: 2 additions & 2 deletions api/v1/weightsandbiases_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ type WeightsAndBiasesSpec struct {

// +kubebuilder:validation:Optional
// +kubebuilder:pruning:PreserveUnknownFields
Release Object `json:"release,omitempty"`
Chart Object `json:"chart,omitempty"`

// +kubebuilder:validation:Optional
// +kubebuilder:pruning:PreserveUnknownFields
Config Object `json:"config,omitempty"`
Values Object `json:"values,omitempty"`
}

// Unstructured values for rendering CDK8s Config.
Expand Down
4 changes: 2 additions & 2 deletions api/v1/zz_generated.deepcopy.go

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

4 changes: 2 additions & 2 deletions config/crd/bases/apps.wandb.com_weightsandbiases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ spec:
spec:
description: WeightsAndBiasesSpec defines the desired state of WeightsAndBiases
properties:
config:
chart:
description: Unstructured values for rendering CDK8s Config.
type: object
x-kubernetes-preserve-unknown-fields: true
release:
values:
description: Unstructured values for rendering CDK8s Config.
type: object
x-kubernetes-preserve-unknown-fields: true
Expand Down
12 changes: 6 additions & 6 deletions controllers/weightsandbiases_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (r *WeightsAndBiasesReconciler) Reconcile(ctx context.Context, req ctrl.Req
userInputSpec, _ := specManager.GetUserInput()
if userInputSpec == nil {
log.Info("No user spec found, creating a new one")
userInputSpec := &spec.Spec{Config: map[string]interface{}{}}
userInputSpec := &spec.Spec{Values: map[string]interface{}{}}
specManager.SetUserInput(userInputSpec)
}

Expand Down Expand Up @@ -127,17 +127,17 @@ func (r *WeightsAndBiasesReconciler) Reconcile(ctx context.Context, req ctrl.Req
hasNotBeenFlaggedForDeletion := wandb.ObjectMeta.DeletionTimestamp.IsZero()

if hasNotBeenFlaggedForDeletion {
if desiredSpec.Release == nil {
if desiredSpec.Chart == nil {
statusManager.Set(status.InvalidConfig)
log.Error(err, "No release type was found in the spec")
return ctrlqueue.DoNotRequeue()
}
t := reflect.TypeOf(desiredSpec.Release)
t := reflect.TypeOf(desiredSpec.Chart)
typ := t.Name()
if t.Kind() == reflect.Ptr {
typ = "*" + t.Elem().Name()
}
log.Info("Found release type "+typ, "release", reflect.TypeOf(desiredSpec.Release))
log.Info("Found release type "+typ, "release", reflect.TypeOf(desiredSpec.Chart))

statusManager.Set(status.Loading)

Expand Down Expand Up @@ -179,8 +179,8 @@ func (r *WeightsAndBiasesReconciler) Reconcile(ctx context.Context, req ctrl.Req
}

if ctrlqueue.ContainsString(wandb.ObjectMeta.Finalizers, resFinalizer) {
if desiredSpec.Release != nil {
log.Info("Deprovisioning", "release", reflect.TypeOf(desiredSpec.Release))
if desiredSpec.Chart != nil {
log.Info("Deprovisioning", "release", reflect.TypeOf(desiredSpec.Chart))
if err := desiredSpec.Prune(ctx, r.Client, wandb, r.Scheme); err != nil {
log.Error(err, "Failed to cleanup deployment.")
} else {
Expand Down
18 changes: 4 additions & 14 deletions pkg/wandb/spec/channel/deployer/deployer.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package deployer

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"

Expand All @@ -18,25 +18,13 @@ func GetURL() string {
return utils.Getenv("DEPLOYER_CHANNEL_URL", DeployerAPI)
}

type Payload struct {
Metadata map[string]string `json:"metadata"`
}

// GetSpec returns the spec for the given license. If the license or an empty
// string it will pull down the latest stable version.
func GetSpec(license string, activeState *spec.Spec) (*spec.Spec, error) {
url := GetURL()
client := &http.Client{}

payload := new(Payload)
if activeState != nil {
// Config can hold secrets. We shouldn't submit it.
payload.Metadata = activeState.Metadata
}

payloadBytes, _ := json.Marshal(payload)
body := bytes.NewReader(payloadBytes)
req, err := http.NewRequest(http.MethodPost, url, body)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
Expand All @@ -54,6 +42,8 @@ func GetSpec(license string, activeState *spec.Spec) (*spec.Spec, error) {
return nil, err
}

fmt.Println("resBody: ", string(resBody))

var spec spec.Spec
err = json.Unmarshal(resBody, &spec)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package release
package charts

import (
"encoding/json"
"fmt"

"github.com/wandb/operator/pkg/wandb/spec"
"github.com/wandb/operator/pkg/wandb/spec/release/helm"
"github.com/wandb/operator/pkg/wandb/spec/charts/helm"
)

func Is(v spec.Validatable, data interface{}) error {
Expand All @@ -26,12 +26,12 @@ func Is(v spec.Validatable, data interface{}) error {
}

type ValidatableRelease interface {
spec.HelmRelease
spec.Chart
spec.Validatable
}

// Get returns tries to match the spec of a given type to a release.
func Get(maybeRelease interface{}) spec.HelmRelease {
func Get(maybeRelease interface{}) spec.Chart {
releases := []ValidatableRelease{
new(helm.LocalRelease),
new(helm.RepoRelease),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (r LocalRelease) Apply(
c client.Client,
wandb *v1.WeightsAndBiases,
scheme *runtime.Scheme,
config spec.Config,
values spec.Values,
) error {
actionableChart, err := r.getActionableChart(wandb)
if err != nil {
Expand All @@ -54,7 +54,7 @@ func (r LocalRelease) Apply(
return err
}

_, err = actionableChart.Apply(chart, config)
_, err = actionableChart.Apply(chart, values)
return err
}

Expand All @@ -63,7 +63,7 @@ func (r LocalRelease) Prune(
c client.Client,
wandb *v1.WeightsAndBiases,
scheme *runtime.Scheme,
_ spec.Config,
_ spec.Values,
) error {
actionableChart, err := r.getActionableChart(wandb)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (r RepoRelease) Apply(
c client.Client,
wandb *v1.WeightsAndBiases,
scheme *runtime.Scheme,
config spec.Config,
config spec.Values,
) error {
local, err := r.ToLocalRelease()
if err != nil {
Expand All @@ -70,7 +70,7 @@ func (r RepoRelease) Prune(
c client.Client,
wandb *v1.WeightsAndBiases,
scheme *runtime.Scheme,
config spec.Config,
config spec.Values,
) error {
local, err := r.ToLocalRelease()
if err != nil {
Expand Down
30 changes: 15 additions & 15 deletions pkg/wandb/spec/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"sigs.k8s.io/yaml"
)

// Config holds an arbitrary tree-like data structure, such as parsed JSON or
// Values holds an arbitrary tree-like data structure, such as parsed JSON or
// YAML. It can be used to represent Helm-like values.
//
// You can use convenient accessors to work with this data structure.
Expand All @@ -22,10 +22,10 @@ import (
// type of the embedded lists and objects. To avoid any error or an unexpected
// behavior use `[]interface{}` for lists and `map[string]interface{}` for
// nested objects.
type Config map[string]interface{}
type Values map[string]interface{}

// AsMap returns the underlying map. This maybe be useful for external libraries.
func (v Config) AsMap() map[string]interface{} {
func (v Values) AsMap() map[string]interface{} {
return v
}

Expand All @@ -34,7 +34,7 @@ func (v Config) AsMap() map[string]interface{} {
//
// It will return an error, if the key does not exist or can not be traversed,
// for example nested keys of a leaf node of the tree.
func (v Config) GetValue(key string) (interface{}, error) {
func (v Values) GetValue(key string) (interface{}, error) {
cursor := v
keyElements := []string{}

Expand Down Expand Up @@ -74,7 +74,7 @@ func (v Config) GetValue(key string) (interface{}, error) {
// or an empty string.
//
// Use `HasKey` to check if the `key` exist.
func (v Config) GetString(key string, defaultValue ...string) string {
func (v Values) GetString(key string, defaultValue ...string) string {
defaultValueToUse := ""
if len(defaultValue) > 0 {
defaultValueToUse = defaultValue[0]
Expand Down Expand Up @@ -102,7 +102,7 @@ func (v Config) GetString(key string, defaultValue ...string) string {
// optional `defaultValue` or `false`.
//
// Use `HasKey` to check if the `key` exist.
func (v Config) GetBool(key string, defaultValue ...bool) bool {
func (v Values) GetBool(key string, defaultValue ...bool) bool {
defaultValueToUse := false
if len(defaultValue) > 0 {
defaultValueToUse = defaultValue[0]
Expand All @@ -121,7 +121,7 @@ func (v Config) GetBool(key string, defaultValue ...bool) bool {
// and it does not support array indexing. When it returns `true` the getter
// methods, e.g. `GetValue`, can successfully retrieve the associated value of
// the key.
func (v Config) HasKey(key string) bool {
func (v Values) HasKey(key string) bool {
if _, err := v.GetValue(key); err == nil {
return true
}
Expand All @@ -136,7 +136,7 @@ func (v Config) HasKey(key string) bool {
// the provided value. It will create any intermediate nested object that
// doesn't exist. But it will return an error when the key can not be traversed,
// for example nested keys of a leaf node of the tree.
func (v Config) SetValue(key string, value interface{}) error {
func (v Values) SetValue(key string, value interface{}) error {
if key == "" {
return errors.Errorf("can not set the root element")
}
Expand Down Expand Up @@ -178,7 +178,7 @@ func (v Config) SetValue(key string, value interface{}) error {
// values as valid values.
//
// It retruns an error if the underlying merge utility encounters an error.
func (v Config) Merge(newValues Config) error {
func (v Values) Merge(newValues Values) error {
if err := mergo.Merge(&v, newValues, mergo.WithOverride); err != nil {
return errors.Wrapf(err, "can not merge new values")
}
Expand All @@ -194,15 +194,15 @@ func (v Config) Merge(newValues Config) error {
//
// It uses Helm SDK coalesce function and does not return an error when fails
// to merge specific entries. Therefore it may lead to an incorrect output.
func (v Config) Coalesce(newValues Config) {
func (v Values) Coalesce(newValues Values) {
chartutil.CoalesceTables(v, newValues)
}

// AddHelmValue sets the specified value using Helm style key and value format.
// This is similar to `--set key=value` command argument of Helm. It does not
// support multiple keys and values. It returns an error if it can not parse
// the key or assign the value.
func (v Config) AddHelmValue(key, value string) error {
func (v Values) AddHelmValue(key, value string) error {
if err := strvals.ParseInto(fmt.Sprintf("%s=%s", key, value), v); err != nil {
return errors.Wrapf(err, "failed to set value: %s=%s", key, value)
}
Expand All @@ -214,16 +214,16 @@ func (v Config) AddHelmValue(key, value string) error {
//
// It will return an error if it fails to parse the YAML content or encounters
// an error while merging. For more details see `Merge` function.
func (v Config) AddFromYAML(content string) error {
func (v Values) AddFromYAML(content string) error {
return v.AddFromYAMLBuffer([]byte(content))
}

// AddFromYAMLBuffer merges the values from the provided YAML.
//
// It will return an error if it fails to parse the YAML content or encounters
// an error while merging. For more details see `Merge` function.
func (v Config) AddFromYAMLBuffer(content []byte) error {
newValues := Config{}
func (v Values) AddFromYAMLBuffer(content []byte) error {
newValues := Values{}

if err := yaml.Unmarshal(content, &newValues); err != nil {
return errors.Wrapf(err, "failed to parse yaml content")
Expand All @@ -236,7 +236,7 @@ func (v Config) AddFromYAMLBuffer(content []byte) error {
//
// It will return an error if it fails to read or parse the YAML file or
// encounters an error while merging. For more details see `Merge` function.
func (v Config) AddFromYAMLFile(filePath string) error {
func (v Values) AddFromYAMLFile(filePath string) error {
fileContent, err := os.ReadFile(filePath)
if err != nil {
return errors.Wrapf(err, "failed to read file: %s", filePath)
Expand Down
10 changes: 5 additions & 5 deletions pkg/wandb/spec/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

v1 "github.com/wandb/operator/api/v1"
"github.com/wandb/operator/pkg/wandb/spec"
"github.com/wandb/operator/pkg/wandb/spec/release"
"github.com/wandb/operator/pkg/wandb/spec/charts"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
)
Expand All @@ -19,8 +19,8 @@ func Defaults(wandb *v1.WeightsAndBiases, scheme *runtime.Scheme) *spec.Spec {
}
gvk, _ := apiutil.GVKForObject(wandb, scheme)
return &spec.Spec{
Release: nil,
Config: map[string]interface{}{
Chart: nil,
Values: map[string]interface{}{
"global": map[string]interface{}{
"operator": map[string]interface{}{
"apiVersion": gvk.GroupVersion().String(),
Expand All @@ -34,7 +34,7 @@ func Defaults(wandb *v1.WeightsAndBiases, scheme *runtime.Scheme) *spec.Spec {
// Spec returns the spec for the given CRD.
func Spec(wandb *v1.WeightsAndBiases) *spec.Spec {
return &spec.Spec{
Release: release.Get(wandb.Spec.Release.Object),
Config: wandb.Spec.Config.Object,
Chart: charts.Get(wandb.Spec.Chart.Object),
Values: wandb.Spec.Values.Object,
}
}
Loading

0 comments on commit 519cd1b

Please sign in to comment.