|
| 1 | +/* |
| 2 | +Copyright 2017 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 | +// Note: the example only works with the code within the same release/branch. |
| 18 | +package main |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "flag" |
| 23 | + "fmt" |
| 24 | + log "github.com/Sirupsen/logrus" |
| 25 | + "os" |
| 26 | + "os/signal" |
| 27 | + "syscall" |
| 28 | + |
| 29 | + apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" |
| 30 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 31 | + "k8s.io/client-go/rest" |
| 32 | + "k8s.io/client-go/tools/clientcmd" |
| 33 | + |
| 34 | + // Uncomment the following line to load the gcp plugin (only required to authenticate against GKE clusters). |
| 35 | + // _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" |
| 36 | + |
| 37 | + crdclient "github.com/crunchydata/postgres-operator/client" |
| 38 | + "github.com/crunchydata/postgres-operator/operator/backup" |
| 39 | + "github.com/crunchydata/postgres-operator/operator/upgrade" |
| 40 | + |
| 41 | + "github.com/crunchydata/postgres-operator/controller" |
| 42 | + "k8s.io/client-go/kubernetes" |
| 43 | +) |
| 44 | + |
| 45 | +var Clientset *kubernetes.Clientset |
| 46 | + |
| 47 | +func main() { |
| 48 | + kubeconfig := flag.String("kubeconfig", "", "Path to a kube config. Only required if out-of-cluster.") |
| 49 | + flag.Parse() |
| 50 | + |
| 51 | + // Create the client config. Use kubeconfig if given, otherwise assume in-cluster. |
| 52 | + config, err := buildConfig(*kubeconfig) |
| 53 | + if err != nil { |
| 54 | + panic(err) |
| 55 | + } |
| 56 | + |
| 57 | + //TODO is this needed any longer? |
| 58 | + apiextensionsclientset, err := apiextensionsclient.NewForConfig(config) |
| 59 | + if err != nil { |
| 60 | + panic(err) |
| 61 | + } |
| 62 | + |
| 63 | + Clientset, err = kubernetes.NewForConfig(config) |
| 64 | + if err != nil { |
| 65 | + log.Info("error creating Clientset") |
| 66 | + panic(err.Error()) |
| 67 | + } |
| 68 | + |
| 69 | + // initialize custom resource using a CustomResourceDefinition if it does not exist |
| 70 | + crd, err := crdclient.CreateCustomResourceDefinition(apiextensionsclientset) |
| 71 | + if err != nil && !apierrors.IsAlreadyExists(err) { |
| 72 | + panic(err) |
| 73 | + } |
| 74 | + if crd != nil { |
| 75 | + fmt.Println(crd.Name + " exists ") |
| 76 | + } |
| 77 | + //defer apiextensionsclientset.ApiextensionsV1beta1().CustomResourceDefinitions().Delete(crd.Name, nil) |
| 78 | + |
| 79 | + clustercrd, err := crdclient.PgclusterCreateCustomResourceDefinition(apiextensionsclientset) |
| 80 | + if err != nil && !apierrors.IsAlreadyExists(err) { |
| 81 | + panic(err) |
| 82 | + } |
| 83 | + if clustercrd != nil { |
| 84 | + fmt.Println(clustercrd.Name + " exists ") |
| 85 | + } |
| 86 | + //defer apiextensionsclientset.ApiextensionsV1beta1().CustomResourceDefinitions().Delete(clustercrd.Name, nil) |
| 87 | + |
| 88 | + backupcrd, err := crdclient.PgbackupCreateCustomResourceDefinition(apiextensionsclientset) |
| 89 | + if err != nil && !apierrors.IsAlreadyExists(err) { |
| 90 | + panic(err) |
| 91 | + } |
| 92 | + if backupcrd != nil { |
| 93 | + fmt.Println(backupcrd.Name + " exists ") |
| 94 | + } |
| 95 | + //defer apiextensionsclientset.ApiextensionsV1beta1().CustomResourceDefinitions().Delete(clustercrd.Name, nil) |
| 96 | + upgradecrd, err := crdclient.PgupgradeCreateCustomResourceDefinition(apiextensionsclientset) |
| 97 | + if err != nil && !apierrors.IsAlreadyExists(err) { |
| 98 | + panic(err) |
| 99 | + } |
| 100 | + if upgradecrd != nil { |
| 101 | + fmt.Println(upgradecrd.Name + " exists ") |
| 102 | + } |
| 103 | + //defer apiextensionsclientset.ApiextensionsV1beta1().CustomResourceDefinitions().Delete(clustercrd.Name, nil) |
| 104 | + policycrd, err := crdclient.PgpolicyCreateCustomResourceDefinition(apiextensionsclientset) |
| 105 | + if err != nil && !apierrors.IsAlreadyExists(err) { |
| 106 | + panic(err) |
| 107 | + } |
| 108 | + if policycrd != nil { |
| 109 | + fmt.Println(policycrd.Name + " exists ") |
| 110 | + } |
| 111 | + policylogcrd, err := crdclient.PgpolicylogCreateCustomResourceDefinition(apiextensionsclientset) |
| 112 | + if err != nil && !apierrors.IsAlreadyExists(err) { |
| 113 | + panic(err) |
| 114 | + } |
| 115 | + if policylogcrd != nil { |
| 116 | + fmt.Println(policylogcrd.Name + " exists ") |
| 117 | + } |
| 118 | + |
| 119 | + // make a new config for our extension's API group, using the first config as a baseline |
| 120 | + crdClient, crdScheme, err := crdclient.NewClient(config) |
| 121 | + if err != nil { |
| 122 | + panic(err) |
| 123 | + } |
| 124 | + |
| 125 | + // start a controller on instances of our custom resource |
| 126 | + |
| 127 | + pgClustercontroller := controller.PgclusterController{ |
| 128 | + PgclusterClient: crdClient, |
| 129 | + PgclusterScheme: crdScheme, |
| 130 | + PgclusterClientset: Clientset, |
| 131 | + } |
| 132 | + pgUpgradecontroller := controller.PgupgradeController{ |
| 133 | + PgupgradeClientset: Clientset, |
| 134 | + PgupgradeClient: crdClient, |
| 135 | + PgupgradeScheme: crdScheme, |
| 136 | + } |
| 137 | + pgBackupcontroller := controller.PgbackupController{ |
| 138 | + PgbackupClient: crdClient, |
| 139 | + PgbackupScheme: crdScheme, |
| 140 | + PgbackupClientset: Clientset, |
| 141 | + } |
| 142 | + pgPolicycontroller := controller.PgpolicyController{ |
| 143 | + PgpolicyClient: crdClient, |
| 144 | + PgpolicyScheme: crdScheme, |
| 145 | + PgpolicyClientset: Clientset, |
| 146 | + } |
| 147 | + pgPolicylogcontroller := controller.PgpolicylogController{ |
| 148 | + PgpolicylogClientset: Clientset, |
| 149 | + PgpolicylogClient: crdClient, |
| 150 | + PgpolicylogScheme: crdScheme, |
| 151 | + } |
| 152 | + |
| 153 | + ctx, cancelFunc := context.WithCancel(context.Background()) |
| 154 | + defer cancelFunc() |
| 155 | + go pgClustercontroller.Run(ctx) |
| 156 | + go pgBackupcontroller.Run(ctx) |
| 157 | + go pgUpgradecontroller.Run(ctx) |
| 158 | + go pgPolicycontroller.Run(ctx) |
| 159 | + go pgPolicylogcontroller.Run(ctx) |
| 160 | + |
| 161 | + Namespace := "default" |
| 162 | + go backup.ProcessJobs(Clientset, crdClient, Namespace) |
| 163 | + go upgrade.MajorUpgradeProcess(Clientset, crdClient, Namespace) |
| 164 | + |
| 165 | + fmt.Print("at end of setup, beginning wait...") |
| 166 | + |
| 167 | + signals := make(chan os.Signal, 1) |
| 168 | + signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM) |
| 169 | + for { |
| 170 | + select { |
| 171 | + case s := <-signals: |
| 172 | + log.Infof("received signal %#v, exiting...\n", s) |
| 173 | + os.Exit(0) |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | +} |
| 178 | + |
| 179 | +func buildConfig(kubeconfig string) (*rest.Config, error) { |
| 180 | + if kubeconfig != "" { |
| 181 | + return clientcmd.BuildConfigFromFlags("", kubeconfig) |
| 182 | + } |
| 183 | + return rest.InClusterConfig() |
| 184 | +} |
0 commit comments