- Notifications
You must be signed in to change notification settings - Fork 604
Add support for private docker image registries #1460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits Select commit Hold shift + click to select a range
31c2d98
Support private docker registries
deliahu f88aee9
Update docker errors
deliahu 85ada7c
Hide docker password from error message
deliahu 764dcc7
Update error message
deliahu 1679151
Add guide
deliahu 36a0d60
Misc cleanup
deliahu cb8f1ab
Update private-docker.md
deliahu a141d2d
Merge branch 'master' into private-docker
RobertLucian b0649d8
Address some PR comments
deliahu e193d55
Address PR comments
deliahu a772fad
Inline docker secret structs
deliahu a26fd6d
Merge branch 'master' into private-docker
vishalbollu 1eefedd
Update private-docker.md
deliahu a78adb8
Merge branch 'private-docker' of github.com:cortexlabs/cortex into pr…
deliahu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Private docker registry | ||
| ||
_WARNING: you are on the master branch, please refer to the docs on the branch that matches your `cortex version`_ | ||
| ||
Until [#1459](https://github.com/cortexlabs/cortex/issues/1459) is addressed, you can use a private docker registry for your Predictor images by following this guide. | ||
| ||
## Local | ||
| ||
When running Cortex locally, you can use private Docker images by running `docker login` and then `docker pull <your_image>`. The Docker image will be present on your machine, and will be accessible by Cortex the next time you run `cortex deploy`. | ||
| ||
## Cluster | ||
| ||
### Step 1 | ||
| ||
Install and configure kubectl ([instructions](kubectl-setup.md)). | ||
| ||
### Step 2 | ||
| ||
Set the following environment variables, replacing the placeholders with your docker username and password: | ||
| ||
```bash | ||
DOCKER_USERNAME=*** | ||
DOCKER_PASSWORD=*** | ||
``` | ||
| ||
Run the following commands: | ||
| ||
```bash | ||
kubectl create secret docker-registry registry-credentials \ | ||
--namespace default \ | ||
--docker-username=$DOCKER_USERNAME \ | ||
--docker-password=$DOCKER_PASSWORD | ||
| ||
kubectl patch serviceaccount default \ | ||
--namespace default \ | ||
-p "{\"imagePullSecrets\": [{\"name\": \"registry-credentials\"}]}" | ||
``` | ||
| ||
### Updating your credentials | ||
| ||
To remove your docker credentials from the cluster, run this command: | ||
| ||
```bash | ||
kubectl delete secret --namespace default registry-credentials | ||
``` | ||
| ||
Then repeat step 2 above with your updated credentials. | ||
| ||
### Removing your credentials | ||
| ||
To remove your docker credentials from the cluster, run the following commands: | ||
| ||
```bash | ||
kubectl delete secret --namespace default registry-credentials | ||
| ||
kubectl patch serviceaccount default \ | ||
--namespace default \ | ||
-p "{\"imagePullSecrets\": []}" | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/* | ||
Copyright 2020 Cortex Labs, Inc. | ||
| ||
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 k8s | ||
| ||
import ( | ||
"context" | ||
| ||
"github.com/cortexlabs/cortex/pkg/lib/errors" | ||
kcore "k8s.io/api/core/v1" | ||
kerrors "k8s.io/apimachinery/pkg/api/errors" | ||
kmeta "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
klabels "k8s.io/apimachinery/pkg/labels" | ||
) | ||
| ||
var _secretTypeMeta = kmeta.TypeMeta{ | ||
APIVersion: "v1", | ||
Kind: "Secret", | ||
} | ||
| ||
type SecretSpec struct { | ||
Name string | ||
Data map[string][]byte | ||
Labels map[string]string | ||
Annotations map[string]string | ||
} | ||
| ||
func Secret(spec *SecretSpec) *kcore.Secret { | ||
secret := &kcore.Secret{ | ||
TypeMeta: _secretTypeMeta, | ||
ObjectMeta: kmeta.ObjectMeta{ | ||
Name: spec.Name, | ||
Labels: spec.Labels, | ||
Annotations: spec.Annotations, | ||
}, | ||
Data: spec.Data, | ||
} | ||
return secret | ||
} | ||
| ||
func (c *Client) CreateSecret(secret *kcore.Secret) (*kcore.Secret, error) { | ||
secret.TypeMeta = _secretTypeMeta | ||
secret, err := c.secretClient.Create(context.Background(), secret, kmeta.CreateOptions{}) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
return secret, nil | ||
} | ||
| ||
func (c *Client) UpdateSecret(secret *kcore.Secret) (*kcore.Secret, error) { | ||
secret.TypeMeta = _secretTypeMeta | ||
secret, err := c.secretClient.Update(context.Background(), secret, kmeta.UpdateOptions{}) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
return secret, nil | ||
} | ||
| ||
func (c *Client) ApplySecret(secret *kcore.Secret) (*kcore.Secret, error) { | ||
existing, err := c.GetSecret(secret.Name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if existing == nil { | ||
return c.CreateSecret(secret) | ||
} | ||
return c.UpdateSecret(secret) | ||
} | ||
| ||
func (c *Client) GetSecret(name string) (*kcore.Secret, error) { | ||
secret, err := c.secretClient.Get(context.Background(), name, kmeta.GetOptions{}) | ||
if kerrors.IsNotFound(err) { | ||
return nil, nil | ||
} | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
secret.TypeMeta = _secretTypeMeta | ||
return secret, nil | ||
} | ||
| ||
func (c *Client) GetSecretData(name string) (map[string][]byte, error) { | ||
secret, err := c.GetSecret(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if secret == nil { | ||
return nil, nil | ||
} | ||
return secret.Data, nil | ||
} | ||
| ||
func (c *Client) DeleteSecret(name string) (bool, error) { | ||
err := c.secretClient.Delete(context.Background(), name, _deleteOpts) | ||
if kerrors.IsNotFound(err) { | ||
deliahu marked this conversation as resolved. Show resolved Hide resolved | ||
return false, nil | ||
} | ||
if err != nil { | ||
return false, errors.WithStack(err) | ||
} | ||
return true, nil | ||
} | ||
| ||
func (c *Client) ListSecrets(opts *kmeta.ListOptions) ([]kcore.Secret, error) { | ||
if opts == nil { | ||
opts = &kmeta.ListOptions{} | ||
} | ||
secretList, err := c.secretClient.List(context.Background(), *opts) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
for i := range secretList.Items { | ||
secretList.Items[i].TypeMeta = _secretTypeMeta | ||
} | ||
return secretList.Items, nil | ||
} | ||
| ||
func (c *Client) ListSecretsByLabels(labels map[string]string) ([]kcore.Secret, error) { | ||
opts := &kmeta.ListOptions{ | ||
LabelSelector: klabels.SelectorFromSet(labels).String(), | ||
} | ||
return c.ListSecrets(opts) | ||
} | ||
| ||
func (c *Client) ListSecretsByLabel(labelKey string, labelValue string) ([]kcore.Secret, error) { | ||
return c.ListSecretsByLabels(map[string]string{labelKey: labelValue}) | ||
} | ||
| ||
func (c *Client) ListSecretsWithLabelKeys(labelKeys ...string) ([]kcore.Secret, error) { | ||
opts := &kmeta.ListOptions{ | ||
LabelSelector: LabelExistsSelector(labelKeys...), | ||
} | ||
return c.ListSecrets(opts) | ||
} | ||
| ||
func SecretMap(secrets []kcore.Secret) map[string]kcore.Secret { | ||
secretMap := map[string]kcore.Secret{} | ||
for _, secret := range secrets { | ||
secretMap[secret.Name] = secret | ||
} | ||
return secretMap | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.