Skip to content

Commit

Permalink
Refactor k8s types (#2462) (#2465)
Browse files Browse the repository at this point in the history
* Refactor types

* Refactor hdf5_test.go

* Format

* Refactor

Co-authored-by: Yusuke Kadowaki <yusuke.kadowaki.1231@gmail.com>
  • Loading branch information
vdaas-ci and ykadowak committed Mar 25, 2024
1 parent 1869f98 commit d9d0723
Show file tree
Hide file tree
Showing 17 changed files with 270 additions and 259 deletions.
10 changes: 5 additions & 5 deletions internal/config/index_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
package config

import "github.com/vdaas/vald/internal/k8s/client"
import "github.com/vdaas/vald/internal/k8s"

// IndexOperator represents the configurations for index k8s operator.
type IndexOperator struct {
Expand Down Expand Up @@ -46,10 +46,10 @@ type IndexOperator struct {
}

type IndexJobTemplates struct {
Rotate *client.Job `json:"rotate" yaml:"rotate"`
Creation *client.Job `json:"creation" yaml:"creation"`
Save *client.Job `json:"save" yaml:"save"`
Correction *client.Job `json:"correction" yaml:"correction"`
Rotate *k8s.Job `json:"rotate" yaml:"rotate"`
Creation *k8s.Job `json:"creation" yaml:"creation"`
Save *k8s.Job `json:"save" yaml:"save"`
Correction *k8s.Job `json:"correction" yaml:"correction"`
}

func (ic *IndexOperator) Bind() *IndexOperator {
Expand Down
64 changes: 13 additions & 51 deletions internal/k8s/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ import (

snapshotv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1"
"github.com/vdaas/vald/internal/errors"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
"github.com/vdaas/vald/internal/k8s"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
Expand All @@ -42,42 +40,6 @@ import (
cli "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

type (
Object = cli.Object
ObjectKey = cli.ObjectKey
DeleteAllOfOptions = cli.DeleteAllOfOptions
DeleteOptions = cli.DeleteOptions
ListOptions = cli.ListOptions
ListOption = cli.ListOption
CreateOption = cli.CreateOption
CreateOptions = cli.CreateOptions
GetOption = cli.GetOption
GetOptions = cli.GetOptions
UpdateOptions = cli.UpdateOptions
MatchingLabels = cli.MatchingLabels
InNamespace = cli.InNamespace
VolumeSnapshot = snapshotv1.VolumeSnapshot
Pod = corev1.Pod
Deployment = appsv1.Deployment
DeploymentList = appsv1.DeploymentList
ObjectMeta = metav1.ObjectMeta
EnvVar = corev1.EnvVar
Job = batchv1.Job
JobList = batchv1.JobList
JobStatus = batchv1.JobStatus
CronJob = batchv1.CronJob
Result = reconcile.Result
)

const (
DeletePropagationBackground = metav1.DeletePropagationBackground
WatchDeletedEvent = watch.Deleted
SelectionOpEquals = selection.Equals
SelectionOpExists = selection.Exists
PodIndexLabel = appsv1.PodIndexLabel
)

var (
Expand All @@ -90,29 +52,29 @@ type Client interface {
// Get retrieves an obj for the given object key from the Kubernetes Cluster.
// obj must be a struct pointer so that obj can be updated with the response
// returned by the Server.
Get(ctx context.Context, name string, namespace string, obj Object, opts ...cli.GetOption) error
Get(ctx context.Context, name string, namespace string, obj k8s.Object, opts ...cli.GetOption) error
// List retrieves list of objects for a given namespace and list options. On a
// successful call, Items field in the list will be populated with the
// result returned from the server.
List(ctx context.Context, list cli.ObjectList, opts ...ListOption) error
List(ctx context.Context, list cli.ObjectList, opts ...k8s.ListOption) error

// Create saves the object obj in the Kubernetes cluster. obj must be a
// struct pointer so that obj can be updated with the content returned by the Server.
Create(ctx context.Context, obj Object, opts ...CreateOption) error
Create(ctx context.Context, obj k8s.Object, opts ...k8s.CreateOption) error

// Delete deletes the given obj from Kubernetes cluster.
Delete(ctx context.Context, obj Object, opts ...cli.DeleteOption) error
Delete(ctx context.Context, obj k8s.Object, opts ...cli.DeleteOption) error

// Update updates the given obj in the Kubernetes cluster. obj must be a
// struct pointer so that obj can be updated with the content returned by the Server.
Update(ctx context.Context, obj Object, opts ...cli.UpdateOption) error
Update(ctx context.Context, obj k8s.Object, opts ...cli.UpdateOption) error

// Patch patches the given obj in the Kubernetes cluster. obj must be a
// struct pointer so that obj can be updated with the content returned by the Server.
Patch(ctx context.Context, obj Object, patch cli.Patch, opts ...cli.PatchOption) error
Patch(ctx context.Context, obj k8s.Object, patch cli.Patch, opts ...cli.PatchOption) error

// Watch watches the given obj for changes and takes the appropriate callbacks.
Watch(ctx context.Context, obj cli.ObjectList, opts ...ListOption) (watch.Interface, error)
Watch(ctx context.Context, obj cli.ObjectList, opts ...k8s.ListOption) (watch.Interface, error)

// MatchingLabels filters the list/delete operation on the given set of labels.
MatchingLabels(labels map[string]string) cli.MatchingLabels
Expand Down Expand Up @@ -171,23 +133,23 @@ func (c *client) List(ctx context.Context, list cli.ObjectList, opts ...cli.List
return c.withWatch.List(ctx, list, opts...)
}

func (c *client) Create(ctx context.Context, obj Object, opts ...CreateOption) error {
func (c *client) Create(ctx context.Context, obj k8s.Object, opts ...k8s.CreateOption) error {
return c.withWatch.Create(ctx, obj, opts...)
}

func (c *client) Delete(ctx context.Context, obj Object, opts ...cli.DeleteOption) error {
func (c *client) Delete(ctx context.Context, obj k8s.Object, opts ...cli.DeleteOption) error {
return c.withWatch.Delete(ctx, obj, opts...)
}

func (c *client) Update(ctx context.Context, obj Object, opts ...cli.UpdateOption) error {
func (c *client) Update(ctx context.Context, obj k8s.Object, opts ...cli.UpdateOption) error {
return c.withWatch.Update(ctx, obj, opts...)
}

func (c *client) Patch(ctx context.Context, obj Object, patch cli.Patch, opts ...cli.PatchOption) error {
func (c *client) Patch(ctx context.Context, obj k8s.Object, patch cli.Patch, opts ...cli.PatchOption) error {
return c.withWatch.Patch(ctx, obj, patch, opts...)
}

func (c *client) Watch(ctx context.Context, obj cli.ObjectList, opts ...ListOption) (watch.Interface, error) {
func (c *client) Watch(ctx context.Context, obj cli.ObjectList, opts ...k8s.ListOption) (watch.Interface, error) {
return c.withWatch.Watch(ctx, obj, opts...)
}

Expand Down
14 changes: 4 additions & 10 deletions internal/k8s/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,17 @@ type reconciler struct {
name string
namespaces []string
onError func(err error)
onReconcile func(ctx context.Context, jobList map[string][]Job)
onReconcile func(ctx context.Context, jobList map[string][]k8s.Job)
listOpts []client.ListOption
jobsByAppNamePool sync.Pool // map[app][]Job
}

// Job is a type alias for the k8s job definition.
type Job = batchv1.Job

// JobStatus is a type alias for the k8s job status definition.
type JobStatus = batchv1.JobStatus

// New returns the JobWatcher that implements reconciliation loop, or any errors occurred.
func New(opts ...Option) (JobWatcher, error) {
r := &reconciler{
jobsByAppNamePool: sync.Pool{
New: func() interface{} {
return make(map[string][]Job)
return make(map[string][]k8s.Job)
},
},
}
Expand Down Expand Up @@ -100,7 +94,7 @@ func (r *reconciler) Reconcile(ctx context.Context, _ reconcile.Request) (res re
return
}

jobs := r.jobsByAppNamePool.Get().(map[string][]Job)
jobs := r.jobsByAppNamePool.Get().(map[string][]k8s.Job)
for idx := range js.Items {
job := js.Items[idx]
name, ok := job.GetObjectMeta().GetLabels()["app"]
Expand All @@ -110,7 +104,7 @@ func (r *reconciler) Reconcile(ctx context.Context, _ reconcile.Request) (res re
}

if _, ok := jobs[name]; !ok {
jobs[name] = make([]Job, 0, len(js.Items))
jobs[name] = make([]k8s.Job, 0, len(js.Items))
}
jobs[name] = append(jobs[name], job)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/k8s/job/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package job
import (
"context"

"github.com/vdaas/vald/internal/k8s"
"sigs.k8s.io/controller-runtime/pkg/manager"
)

Expand Down Expand Up @@ -57,7 +58,7 @@ func WithOnErrorFunc(f func(err error)) Option {
}

// WithOnReconcileFunc returns Option that sets r.onReconcile.
func WithOnReconcileFunc(f func(ctx context.Context, jobList map[string][]Job)) Option {
func WithOnReconcileFunc(f func(ctx context.Context, jobList map[string][]k8s.Job)) Option {
return func(r *reconciler) error {
r.onReconcile = f
return nil
Expand Down
5 changes: 0 additions & 5 deletions internal/k8s/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

type (
Manager = manager.Manager
OwnerReference = metav1.OwnerReference
)

type Controller interface {
Start(ctx context.Context) (<-chan error, error)
GetManager() Manager
Expand Down
72 changes: 72 additions & 0 deletions internal/k8s/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// Copyright (C) 2019-2024 vdaas.org vald team <vald@vdaas.org>
//
// 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
//
// https://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 k8s

import (
snapshotv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/watch"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

type (
Object = client.Object
ObjectKey = client.ObjectKey
DeleteAllOfOptions = client.DeleteAllOfOptions
DeleteOptions = client.DeleteOptions
ListOptions = client.ListOptions
ListOption = client.ListOption
CreateOption = client.CreateOption
CreateOptions = client.CreateOptions
GetOption = client.GetOption
GetOptions = client.GetOptions
UpdateOptions = client.UpdateOptions
MatchingLabels = client.MatchingLabels
InNamespace = client.InNamespace
VolumeSnapshot = snapshotv1.VolumeSnapshot
VolumeSnapshotList = snapshotv1.VolumeSnapshotList
Pod = corev1.Pod
Deployment = appsv1.Deployment
DeploymentList = appsv1.DeploymentList
ObjectMeta = metav1.ObjectMeta
EnvVar = corev1.EnvVar
Job = batchv1.Job
JobList = batchv1.JobList
JobStatus = batchv1.JobStatus
CronJob = batchv1.CronJob
Result = reconcile.Result
OwnerReference = metav1.OwnerReference
PersistentVolumeClaim = corev1.PersistentVolumeClaim
PersistentVolumeClaimList = corev1.PersistentVolumeClaimList
PersistentVolumeClaimSpec = corev1.PersistentVolumeClaimSpec
TypedLocalObjectReference = corev1.TypedLocalObjectReference
Manager = manager.Manager
)

const (
DeletePropagationBackground = metav1.DeletePropagationBackground
WatchDeletedEvent = watch.Deleted
SelectionOpEquals = selection.Equals
SelectionOpExists = selection.Exists
PodIndexLabel = appsv1.PodIndexLabel
)
8 changes: 4 additions & 4 deletions internal/k8s/vald/benchmark/job/job_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package job

import (
jobs "github.com/vdaas/vald/internal/k8s/job"
"github.com/vdaas/vald/internal/k8s"
corev1 "k8s.io/api/core/v1"
)

Expand All @@ -42,14 +42,14 @@ const (
)

type BenchmarkJobTpl interface {
CreateJobTpl(opts ...BenchmarkJobOption) (jobs.Job, error)
CreateJobTpl(opts ...BenchmarkJobOption) (k8s.Job, error)
}

type benchmarkJobTpl struct {
containerName string
containerImageName string
imagePullPolicy ImagePullPolicy
jobTpl jobs.Job
jobTpl k8s.Job
}

func NewBenchmarkJob(opts ...BenchmarkJobTplOption) (BenchmarkJobTpl, error) {
Expand All @@ -63,7 +63,7 @@ func NewBenchmarkJob(opts ...BenchmarkJobTplOption) (BenchmarkJobTpl, error) {
return bjTpl, nil
}

func (b *benchmarkJobTpl) CreateJobTpl(opts ...BenchmarkJobOption) (jobs.Job, error) {
func (b *benchmarkJobTpl) CreateJobTpl(opts ...BenchmarkJobOption) (k8s.Job, error) {
for _, opt := range append(defaultBenchmarkJobOpts, opts...) {
err := opt(&b.jobTpl)
if err != nil {
Expand Down
Loading

0 comments on commit d9d0723

Please sign in to comment.