Implement an Aggregated Apiserver Using Local Files as Storage
Learn how to build an aggregated apiserver adopting local files as storage rather than etcd v3.
We'll cover the following...
Overview
Building an aggregated apiserver from scratch is hard. Luckily, we’ve got the development kit apiserver-boot
, which is shipped by the Kubernetes SIGs community. With this tool, we can easily build our own scaffold project. Below is the scaffold project pwk
that we built using the apiserver-boot
.
/* Copyright 2022. 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 ( "k8s.io/klog" "sigs.k8s.io/apiserver-runtime/pkg/builder" "sigs.k8s.io/apiserver-runtime/pkg/experimental/storage/filepath" // +kubebuilder:scaffold:resource-imports barv1beta1 "educative.io/pwk/pkg/apis/bar/v1beta1" ) func main() { err := builder.APIServer. // +kubebuilder:scaffold:resource-register // writes Foo resources as static files under the "data" folder in the working directory. WithResourceAndHandler(&barv1beta1.Foo{}, filepath.NewJSONFilepathStorageProvider(&barv1beta1.Foo{}, "data")). WithoutEtcd(). Execute() if err != nil { klog.Fatal(err) } }
Now, let’s implement our own aggregated apiserver by building on top of this scaffold project.
In this lesson, we’re going to define a new API kind Foo
served by the aggregated apiserver, and we’ll have a controller to reconcile the Foo
kind objects.
To get started, hit the “Run” button to initialize our development environment in the terminal above.
Add new fields
We add new fields into the Foo
struct, as shown below. The modified file is ...