mirror of
https://github.com/1Password/onepassword-operator.git
synced 2025-10-23 16:00:46 +00:00

* Move controller package inside internal directory Based on the go/v4 project structure, the following changed: - Pakcage `controllers` is now named `controller` - Package `controller` now lives inside new `internal` directory * Move main.go in cmd directory Based on the new go/v4 project structure, `main.go` now lives in the `cmd` directory. * Change package import in main.go * Update go mod dependencies Update the dependencies based on the versions obtained by creating a new operator project using `kubebuilder init --domain onepassword.com --plugins=go/v4`. This is based on the migration steps provided to go from go/v3 to go/v4 (https://book.kubebuilder.io/migration/migration_guide_gov3_to_gov4) * Update vendor * Adjust code for breaking changes from pkg update sigs.k8s.io/controller-runtime package had breaking changes from v0.14.5 to v0.16.3. This commit brings the changes needed to achieve the same things using the new functionality avaialble. * Adjust paths to connect yaml files Since `main.go` is now in `cmd` directory, the paths to the files for deploying Connect have to be adjusted based on the new location `main.go` is executed from. * Update files based on new structure and scaffolding These changes are made based on the new project structure and scaffolding obtained when using the new go/v4 project structure. These were done based on the migration steps mentioned when migrating to go/v4 (https://book.kubebuilder.io/migration/migration_guide_gov3_to_gov4). * Update config files These updates are made based on the Kustomize v4 syntax. This is part of the upgrate to go/v4 (https://book.kubebuilder.io/migration/migration_guide_gov3_to_gov4) * Update dependencies and GO version * Update vendor * Update Kubernetes tools versions * Update operator version in Makefile Now the version in the Makefile matches the version of the operator * Update Operator SDK version in version.go * Adjust generated deepcopy It seems that the +build tag is no longer needed based on the latest generated scaffolding, therefore it's removed. * Update copyright year * Bring back missing changes from migration Some customization in Makefile was lost during the migration process. Specifically, the namespace customization for `make deploy` command. Also, we push changes to kustomization.yaml for making the deploy process smoother. * Add RBAC perms for coordination.k8s.io It seems that with the latest changes to Kubernetes and Kustomize, we need to add additional RBAC to the service account used so that it can properly access the `leases` resource. * Optimize Dockerfile Dockerfile had a step for caching dependencies (go mod download). However, this is already done by the vendor directory, which we include. Therefore, this step can be removed to make the image build time faster.
203 lines
6.6 KiB
Go
203 lines
6.6 KiB
Go
/*
|
|
Copyright 2014 The Kubernetes Authors.
|
|
|
|
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 rest
|
|
|
|
import (
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
"k8s.io/apimachinery/pkg/types"
|
|
"k8s.io/client-go/util/flowcontrol"
|
|
)
|
|
|
|
const (
|
|
// Environment variables: Note that the duration should be long enough that the backoff
|
|
// persists for some reasonable time (i.e. 120 seconds). The typical base might be "1".
|
|
envBackoffBase = "KUBE_CLIENT_BACKOFF_BASE"
|
|
envBackoffDuration = "KUBE_CLIENT_BACKOFF_DURATION"
|
|
)
|
|
|
|
// Interface captures the set of operations for generically interacting with Kubernetes REST apis.
|
|
type Interface interface {
|
|
GetRateLimiter() flowcontrol.RateLimiter
|
|
Verb(verb string) *Request
|
|
Post() *Request
|
|
Put() *Request
|
|
Patch(pt types.PatchType) *Request
|
|
Get() *Request
|
|
Delete() *Request
|
|
APIVersion() schema.GroupVersion
|
|
}
|
|
|
|
// ClientContentConfig controls how RESTClient communicates with the server.
|
|
//
|
|
// TODO: ContentConfig will be updated to accept a Negotiator instead of a
|
|
// NegotiatedSerializer and NegotiatedSerializer will be removed.
|
|
type ClientContentConfig struct {
|
|
// AcceptContentTypes specifies the types the client will accept and is optional.
|
|
// If not set, ContentType will be used to define the Accept header
|
|
AcceptContentTypes string
|
|
// ContentType specifies the wire format used to communicate with the server.
|
|
// This value will be set as the Accept header on requests made to the server if
|
|
// AcceptContentTypes is not set, and as the default content type on any object
|
|
// sent to the server. If not set, "application/json" is used.
|
|
ContentType string
|
|
// GroupVersion is the API version to talk to. Must be provided when initializing
|
|
// a RESTClient directly. When initializing a Client, will be set with the default
|
|
// code version. This is used as the default group version for VersionedParams.
|
|
GroupVersion schema.GroupVersion
|
|
// Negotiator is used for obtaining encoders and decoders for multiple
|
|
// supported media types.
|
|
Negotiator runtime.ClientNegotiator
|
|
}
|
|
|
|
// RESTClient imposes common Kubernetes API conventions on a set of resource paths.
|
|
// The baseURL is expected to point to an HTTP or HTTPS path that is the parent
|
|
// of one or more resources. The server should return a decodable API resource
|
|
// object, or an api.Status object which contains information about the reason for
|
|
// any failure.
|
|
//
|
|
// Most consumers should use client.New() to get a Kubernetes API client.
|
|
type RESTClient struct {
|
|
// base is the root URL for all invocations of the client
|
|
base *url.URL
|
|
// versionedAPIPath is a path segment connecting the base URL to the resource root
|
|
versionedAPIPath string
|
|
|
|
// content describes how a RESTClient encodes and decodes responses.
|
|
content ClientContentConfig
|
|
|
|
// creates BackoffManager that is passed to requests.
|
|
createBackoffMgr func() BackoffManager
|
|
|
|
// rateLimiter is shared among all requests created by this client unless specifically
|
|
// overridden.
|
|
rateLimiter flowcontrol.RateLimiter
|
|
|
|
// warningHandler is shared among all requests created by this client.
|
|
// If not set, defaultWarningHandler is used.
|
|
warningHandler WarningHandler
|
|
|
|
// Set specific behavior of the client. If not set http.DefaultClient will be used.
|
|
Client *http.Client
|
|
}
|
|
|
|
// NewRESTClient creates a new RESTClient. This client performs generic REST functions
|
|
// such as Get, Put, Post, and Delete on specified paths.
|
|
func NewRESTClient(baseURL *url.URL, versionedAPIPath string, config ClientContentConfig, rateLimiter flowcontrol.RateLimiter, client *http.Client) (*RESTClient, error) {
|
|
if len(config.ContentType) == 0 {
|
|
config.ContentType = "application/json"
|
|
}
|
|
|
|
base := *baseURL
|
|
if !strings.HasSuffix(base.Path, "/") {
|
|
base.Path += "/"
|
|
}
|
|
base.RawQuery = ""
|
|
base.Fragment = ""
|
|
|
|
return &RESTClient{
|
|
base: &base,
|
|
versionedAPIPath: versionedAPIPath,
|
|
content: config,
|
|
createBackoffMgr: readExpBackoffConfig,
|
|
rateLimiter: rateLimiter,
|
|
|
|
Client: client,
|
|
}, nil
|
|
}
|
|
|
|
// GetRateLimiter returns rate limiter for a given client, or nil if it's called on a nil client
|
|
func (c *RESTClient) GetRateLimiter() flowcontrol.RateLimiter {
|
|
if c == nil {
|
|
return nil
|
|
}
|
|
return c.rateLimiter
|
|
}
|
|
|
|
// readExpBackoffConfig handles the internal logic of determining what the
|
|
// backoff policy is. By default if no information is available, NoBackoff.
|
|
// TODO Generalize this see #17727 .
|
|
func readExpBackoffConfig() BackoffManager {
|
|
backoffBase := os.Getenv(envBackoffBase)
|
|
backoffDuration := os.Getenv(envBackoffDuration)
|
|
|
|
backoffBaseInt, errBase := strconv.ParseInt(backoffBase, 10, 64)
|
|
backoffDurationInt, errDuration := strconv.ParseInt(backoffDuration, 10, 64)
|
|
if errBase != nil || errDuration != nil {
|
|
return &NoBackoff{}
|
|
}
|
|
return &URLBackoff{
|
|
Backoff: flowcontrol.NewBackOff(
|
|
time.Duration(backoffBaseInt)*time.Second,
|
|
time.Duration(backoffDurationInt)*time.Second)}
|
|
}
|
|
|
|
// Verb begins a request with a verb (GET, POST, PUT, DELETE).
|
|
//
|
|
// Example usage of RESTClient's request building interface:
|
|
// c, err := NewRESTClient(...)
|
|
// if err != nil { ... }
|
|
// resp, err := c.Verb("GET").
|
|
//
|
|
// Path("pods").
|
|
// SelectorParam("labels", "area=staging").
|
|
// Timeout(10*time.Second).
|
|
// Do()
|
|
//
|
|
// if err != nil { ... }
|
|
// list, ok := resp.(*api.PodList)
|
|
func (c *RESTClient) Verb(verb string) *Request {
|
|
return NewRequest(c).Verb(verb)
|
|
}
|
|
|
|
// Post begins a POST request. Short for c.Verb("POST").
|
|
func (c *RESTClient) Post() *Request {
|
|
return c.Verb("POST")
|
|
}
|
|
|
|
// Put begins a PUT request. Short for c.Verb("PUT").
|
|
func (c *RESTClient) Put() *Request {
|
|
return c.Verb("PUT")
|
|
}
|
|
|
|
// Patch begins a PATCH request. Short for c.Verb("Patch").
|
|
func (c *RESTClient) Patch(pt types.PatchType) *Request {
|
|
return c.Verb("PATCH").SetHeader("Content-Type", string(pt))
|
|
}
|
|
|
|
// Get begins a GET request. Short for c.Verb("GET").
|
|
func (c *RESTClient) Get() *Request {
|
|
return c.Verb("GET")
|
|
}
|
|
|
|
// Delete begins a DELETE request. Short for c.Verb("DELETE").
|
|
func (c *RESTClient) Delete() *Request {
|
|
return c.Verb("DELETE")
|
|
}
|
|
|
|
// APIVersion returns the APIVersion this RESTClient is expected to use.
|
|
func (c *RESTClient) APIVersion() schema.GroupVersion {
|
|
return c.content.GroupVersion
|
|
}
|