Skip to content

Commit

Permalink
Merge pull request #4 from cybozu-go/add-imageprefetch-crd
Browse files Browse the repository at this point in the history
Create a template for the ImagePrefetch CRD
  • Loading branch information
zeroalphat authored Mar 3, 2025
2 parents 65c7bd4 + cb87eb7 commit 82f6db3
Show file tree
Hide file tree
Showing 20 changed files with 772 additions and 45 deletions.
12 changes: 11 additions & 1 deletion PROJECT
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
domain: ofen.cybozu.io
domain: cybozu.io
layout:
- go.kubebuilder.io/v4
projectName: ofen
repo: github.com/cybozu-go/ofen
resources:
- api:
crdVersion: v1
namespaced: true
controller: true
domain: cybozu.io
group: ofen
kind: ImagePrefetch
path: github.com/cybozu-go/ofen/api/v1
version: v1
version: "3"
20 changes: 20 additions & 0 deletions api/v1/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Package v1 contains API Schema definitions for the ofen v1 API group
// +kubebuilder:object:generate=true
// +groupName=ofen.cybozu.io
package v1

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "ofen.cybozu.io", Version: "v1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)
88 changes: 88 additions & 0 deletions api/v1/imageprefetch_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package v1

import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// ImagePrefetchSpec defines the desired state of ImagePrefetch
type ImagePrefetchSpec struct {
// Images is a list of container images that will be pre-downloaded to the target nodes
Images []string `json:"images"`

// NodeSelector is a map of key-value pairs that specify which nodes should have the images pre-downloaded
// +optional
NodeSelector map[string]string `json:"nodeSelector,omitempty"`

// Replicas is the number of nodes that should download the specified images
// +optional
Replicas int `json:"replicas,omitempty"`

// ImagePullSecrets is a list of secret names that contain credentials for authenticating with container registries
// +optional
ImagePullSecrets []corev1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
}

// ImagePrefetchStatus defines the observed state of ImagePrefetch
type ImagePrefetchStatus struct {
// The generation observed by the controller.
// +optional
ObservedGeneration int64 `json:"observedGeneration,omitempty"`

// Conditions represent the latest available observations of an object's state
// +listType=map
// +listMapKey=type
// +optional
Conditions []metav1.Condition `json:"conditions,omitempty"`

// DesiredNodes represents the number of nodes that should have the images pre-downloaded
// +optional
// +kubebuilder:default:=0
DesiredNodes int `json:"desiredNodes,omitempty"`

// ImagePulledNodes represents the number of nodes that have successfully pre-downloaded the images
// +optional
// +kubebuilder:default:=0
ImagePulledNodes int `json:"imagePulledNodes,omitempty"`

// ImagePullingNodes represents the number of nodes that are currently downloading the images
// +optional
// +kubebuilder:default:=0
ImagePullingNodes int `json:"imagePullingNodes,omitempty"`

// ImagePullFailedNodes represents the number of nodes that failed to download the images
// +optional
// +kubebuilder:default:=0
ImagePullFailedNodes int `json:"imagePullFailedNodes,omitempty"`
}

const (
ConditionReady = "Ready"
ConditionProgressing = "Progressing"
ConditionImageDownloadFailed = "ImageDownloadFailed"
)

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status

// ImagePrefetch is the Schema for the imageprefetches API
type ImagePrefetch struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec ImagePrefetchSpec `json:"spec,omitempty"`
Status ImagePrefetchStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// ImagePrefetchList contains a list of ImagePrefetch
type ImagePrefetchList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []ImagePrefetch `json:"items"`
}

func init() {
SchemeBuilder.Register(&ImagePrefetch{}, &ImagePrefetchList{})
}
124 changes: 124 additions & 0 deletions api/v1/zz_generated.deepcopy.go

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

27 changes: 11 additions & 16 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
/*
Copyright 2025.
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 main

import (
Expand All @@ -32,6 +16,9 @@ import (
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"

ofenv1 "github.com/cybozu-go/ofen/api/v1"
"github.com/cybozu-go/ofen/internal/controller"
// +kubebuilder:scaffold:imports
)

Expand All @@ -43,6 +30,7 @@ var (
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))

utilruntime.Must(ofenv1.AddToScheme(scheme))
// +kubebuilder:scaffold:scheme
}

Expand Down Expand Up @@ -138,6 +126,13 @@ func main() {
os.Exit(1)
}

if err = (&controller.ImagePrefetchReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ImagePrefetch")
os.Exit(1)
}
// +kubebuilder:scaffold:builder

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
Expand Down
Loading

0 comments on commit 82f6db3

Please sign in to comment.