Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport PR #2462 to release/v1.7 for Refactor k8s types #2465

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

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 @@
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 @@
// 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 @@
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 {

Check warning on line 136 in internal/k8s/client/client.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/client/client.go#L136

Added line #L136 was not covered by tests
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 {

Check warning on line 140 in internal/k8s/client/client.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/client/client.go#L140

Added line #L140 was not covered by tests
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 {

Check warning on line 144 in internal/k8s/client/client.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/client/client.go#L144

Added line #L144 was not covered by tests
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 {

Check warning on line 148 in internal/k8s/client/client.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/client/client.go#L148

Added line #L148 was not covered by tests
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) {

Check warning on line 152 in internal/k8s/client/client.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/client/client.go#L152

Added line #L152 was not covered by tests
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 @@
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)

Check warning on line 53 in internal/k8s/job/job.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/job/job.go#L53

Added line #L53 was not covered by tests
},
},
}
Expand Down Expand Up @@ -100,7 +94,7 @@
return
}

jobs := r.jobsByAppNamePool.Get().(map[string][]Job)
jobs := r.jobsByAppNamePool.Get().(map[string][]k8s.Job)

Check warning on line 97 in internal/k8s/job/job.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/job/job.go#L97

Added line #L97 was not covered by tests
for idx := range js.Items {
job := js.Items[idx]
name, ok := job.GetObjectMeta().GetLabels()["app"]
Expand All @@ -110,7 +104,7 @@
}

if _, ok := jobs[name]; !ok {
jobs[name] = make([]Job, 0, len(js.Items))
jobs[name] = make([]k8s.Job, 0, len(js.Items))

Check warning on line 107 in internal/k8s/job/job.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/job/job.go#L107

Added line #L107 was not covered by tests
}
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 @@
import (
"context"

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

Expand Down Expand Up @@ -57,7 +58,7 @@
}

// 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 {

Check warning on line 61 in internal/k8s/job/option.go

View check run for this annotation

Codecov / codecov/patch

internal/k8s/job/option.go#L61

Added line #L61 was not covered by tests
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 <[email protected]>
//
// 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
Loading