| 1 |
/* |
| 2 |
Copyright 2016 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package factory |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"fmt" |
| 22 |
|
| 23 |
"k8s.io/apimachinery/pkg/runtime" |
| 24 |
"k8s.io/apiserver/pkg/storage" |
| 25 |
"k8s.io/apiserver/pkg/storage/etcd3/metrics" |
| 26 |
"k8s.io/apiserver/pkg/storage/storagebackend" |
| 27 |
) |
| 28 |
|
| 29 |
// DestroyFunc is to destroy any resources used by the storage returned in Create() together. |
| 30 |
type DestroyFunc func() |
| 31 |
|
| 32 |
// Create creates a storage backend based on given config. |
| 33 |
func Create(c storagebackend.ConfigForResource, newFunc, newListFunc func() runtime.Object, resourcePrefix string) (storage.Interface, DestroyFunc, error) { |
| 34 |
switch c.Type { |
| 35 |
case storagebackend.StorageTypeETCD2: |
| 36 |
return nil, nil, fmt.Errorf("%s is no longer a supported storage backend", c.Type) |
| 37 |
case storagebackend.StorageTypeUnset, storagebackend.StorageTypeETCD3: |
| 38 |
return newETCD3Storage(c, newFunc, newListFunc, resourcePrefix) |
| 39 |
default: |
| 40 |
return nil, nil, fmt.Errorf("unknown storage type: %s", c.Type) |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
// CreateHealthCheck creates a healthcheck function based on given config. |
| 45 |
func CreateHealthCheck(c storagebackend.Config, stopCh <-chan struct{}) (func() error, error) { |
| 46 |
switch c.Type { |
| 47 |
case storagebackend.StorageTypeETCD2: |
| 48 |
return nil, fmt.Errorf("%s is no longer a supported storage backend", c.Type) |
| 49 |
case storagebackend.StorageTypeUnset, storagebackend.StorageTypeETCD3: |
| 50 |
return newETCD3HealthCheck(c, stopCh) |
| 51 |
default: |
| 52 |
return nil, fmt.Errorf("unknown storage type: %s", c.Type) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
func CreateReadyCheck(c storagebackend.Config, stopCh <-chan struct{}) (func() error, error) { |
| 57 |
switch c.Type { |
| 58 |
case storagebackend.StorageTypeETCD2: |
| 59 |
return nil, fmt.Errorf("%s is no longer a supported storage backend", c.Type) |
| 60 |
case storagebackend.StorageTypeUnset, storagebackend.StorageTypeETCD3: |
| 61 |
return newETCD3ReadyCheck(c, stopCh) |
| 62 |
default: |
| 63 |
return nil, fmt.Errorf("unknown storage type: %s", c.Type) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
func CreateProber(c storagebackend.Config) (Prober, error) { |
| 68 |
switch c.Type { |
| 69 |
case storagebackend.StorageTypeETCD2: |
| 70 |
return nil, fmt.Errorf("%s is no longer a supported storage backend", c.Type) |
| 71 |
case storagebackend.StorageTypeUnset, storagebackend.StorageTypeETCD3: |
| 72 |
return newETCD3ProberMonitor(c) |
| 73 |
default: |
| 74 |
return nil, fmt.Errorf("unknown storage type: %s", c.Type) |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
func CreateMonitor(c storagebackend.Config) (metrics.Monitor, error) { |
| 79 |
switch c.Type { |
| 80 |
case storagebackend.StorageTypeETCD2: |
| 81 |
return nil, fmt.Errorf("%s is no longer a supported storage backend", c.Type) |
| 82 |
case storagebackend.StorageTypeUnset, storagebackend.StorageTypeETCD3: |
| 83 |
return newETCD3ProberMonitor(c) |
| 84 |
default: |
| 85 |
return nil, fmt.Errorf("unknown storage type: %s", c.Type) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
// Prober is an interface that defines the Probe function for doing etcd readiness/liveness checks. |
| 90 |
type Prober interface { |
| 91 |
Probe(ctx context.Context) error |
| 92 |
Close() error |
| 93 |
} |