<commit_before>/*

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 katibclient

import (
	"context"

	apiv1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/errors"
	"k8s.io/apimachinery/pkg/types"
	"k8s.io/client-go/kubernetes/scheme"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/client/config"

	experimentsv1alpha3 "github.com/kubeflow/katib/pkg/apis/controller/experiments/v1alpha3"
	suggestionsv1alpha3 "github.com/kubeflow/katib/pkg/apis/controller/suggestions/v1alpha3"
	trialsv1alpha3 "github.com/kubeflow/katib/pkg/apis/controller/trials/v1alpha3"
	"github.com/kubeflow/katib/pkg/controller.v1alpha3/consts"
)

type Client interface {
	InjectClient(c client.Client)
	GetClient() client.Client
	GetExperimentList(namespace ...string) (*experimentsv1alpha3.ExperimentList, error)
	CreateExperiment(experiment *experimentsv1alpha3.Experiment, namespace ...string) error
	UpdateExperiment(experiment *experimentsv1alpha3.Experiment, namespace ...string) error
	DeleteExperiment(experiment *experimentsv1alpha3.Experiment, namespace ...string) error
	GetExperiment(name string, namespace ...string) (*experimentsv1alpha3.Experiment, error)
	GetConfigMap(name string, namespace ...string) (map[string]string, error)
	GetTrialList(name string, namespace ...string) (*trialsv1alpha3.TrialList, error)
	GetTrialTemplates(namespace ...string) (map[string]string, error)
	GetSuggestion(name string, namespace ...string) (*suggestionsv1alpha3.Suggestion, error)
	UpdateTrialTemplates(newTrialTemplates map[string]string, namespace ...string) error
	GetNamespaceList() (*apiv1.NamespaceList, error)
}

type KatibClient struct {
	client client.Client
}

func NewWithGivenClient(c client.Client) Client {
	return &KatibClient{
		client: c,
	}
}

func NewClient(options client.Options) (Client, error) {
	cfg, err := config.GetConfig()
	if err != nil {
		return nil, err
	}
	experimentsv1alpha3.AddToScheme(scheme.Scheme)
	trialsv1alpha3.AddToScheme(scheme.Scheme)
	suggestionsv1alpha3.AddToScheme(scheme.Scheme)
	cl, err := client.New(cfg, options)
	return &KatibClient{
		client: cl,
	}, nil
}

func (k *KatibClient) InjectClient(c client.Client) {
	k.client = c
}

func (k *KatibClient) GetClient() client.Client {
	return k.client
}

func (k *KatibClient) GetExperimentList(namespace ...string) (*experimentsv1alpha3.ExperimentList, error) {
	ns := getNamespace(namespace...)
	expList := &experimentsv1alpha3.ExperimentList{}
	listOpt := client.InNamespace(ns)

	if err := k.client.List(context.Background(), listOpt, expList); err != nil {
		return expList, err
	}
	return expList, nil
<commit_msg>Adding error propagation for K8s client creation in KatibClient (#1053)<commit_after>/*

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 katibclient

import (
	"context"

	apiv1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/errors"
	"k8s.io/apimachinery/pkg/types"
	"k8s.io/client-go/kubernetes/scheme"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/client/config"

	experimentsv1alpha3 "github.com/kubeflow/katib/pkg/apis/controller/experiments/v1alpha3"
	suggestionsv1alpha3 "github.com/kubeflow/katib/pkg/apis/controller/suggestions/v1alpha3"
	trialsv1alpha3 "github.com/kubeflow/katib/pkg/apis/controller/trials/v1alpha3"
	"github.com/kubeflow/katib/pkg/controller.v1alpha3/consts"
)

type Client interface {
	InjectClient(c client.Client)
	GetClient() client.Client
	GetExperimentList(namespace ...string) (*experimentsv1alpha3.ExperimentList, error)
	CreateExperiment(experiment *experimentsv1alpha3.Experiment, namespace ...string) error
	UpdateExperiment(experiment *experimentsv1alpha3.Experiment, namespace ...string) error
	DeleteExperiment(experiment *experimentsv1alpha3.Experiment, namespace ...string) error
	GetExperiment(name string, namespace ...string) (*experimentsv1alpha3.Experiment, error)
	GetConfigMap(name string, namespace ...string) (map[string]string, error)
	GetTrialList(name string, namespace ...string) (*trialsv1alpha3.TrialList, error)
	GetTrialTemplates(namespace ...string) (map[string]string, error)
	GetSuggestion(name string, namespace ...string) (*suggestionsv1alpha3.Suggestion, error)
	UpdateTrialTemplates(newTrialTemplates map[string]string, namespace ...string) error
	GetNamespaceList() (*apiv1.NamespaceList, error)
}

type KatibClient struct {
	client client.Client
}

func NewWithGivenClient(c client.Client) Client {
	return &KatibClient{
		client: c,
	}
}

func NewClient(options client.Options) (Client, error) {
	cfg, err := config.GetConfig()
	if err != nil {
		return nil, err
	}
	experimentsv1alpha3.AddToScheme(scheme.Scheme)
	trialsv1alpha3.AddToScheme(scheme.Scheme)
	suggestionsv1alpha3.AddToScheme(scheme.Scheme)
	cl, err := client.New(cfg, options)
	if err != nil {
		return nil, err
	}
	return &KatibClient{
		client: cl,
	}, nil
}

func (k *KatibClient) InjectClient(c client.Client) {
	k.client = c
}

func (k *KatibClient) GetClient() client.Client {
	return k.client
}

func (k *KatibClient) GetExperimentList(namespace ...string) (*experimentsv1alpha3.ExperimentList, error) {
	ns := getNamespace(namespace...)
	expList := &experimentsv1alpha3.ExperimentList{}
	listOpt := client.InNamespace(ns)

	if err := k.client.List(context.Background(), listOpt, expList); err != nil {
		return expList, err
	}
	return expList, nil

{'fraction_non_alphanumeric': 0.11289813938820562, 'fraction_numerical': 0.016871649321980448, 'mean_word_length': 5.4135490394337715, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2970535', 'n_tokens_mistral': 2153, 'n_tokens_neox': 2085, 'n_words': 491}