Skip to content
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

Allow getting logs directly from deployment, job and statefulset #40927

Merged
merged 2 commits into from Feb 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 14 additions & 14 deletions federation/pkg/kubefed/init/init.go
Expand Up @@ -377,7 +377,7 @@ func (i *initFederation) Run(cmdOut io.Writer, config util.AdminConfig) error {
return err
}

func createNamespace(clientset *client.Clientset, namespace string, dryRun bool) (*api.Namespace, error) {
func createNamespace(clientset client.Interface, namespace string, dryRun bool) (*api.Namespace, error) {
ns := &api.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
Expand All @@ -391,7 +391,7 @@ func createNamespace(clientset *client.Clientset, namespace string, dryRun bool)
return clientset.Core().Namespaces().Create(ns)
}

func createService(clientset *client.Clientset, namespace, svcName, apiserverAdvertiseAddress string, apiserverServiceType v1.ServiceType, dryRun bool) (*api.Service, []string, []string, error) {
func createService(clientset client.Interface, namespace, svcName, apiserverAdvertiseAddress string, apiserverServiceType v1.ServiceType, dryRun bool) (*api.Service, []string, []string, error) {
svc := &api.Service{
ObjectMeta: metav1.ObjectMeta{
Name: svcName,
Expand Down Expand Up @@ -436,12 +436,12 @@ func createService(clientset *client.Clientset, namespace, svcName, apiserverAdv
return svc, ips, hostnames, err
}

func getClusterNodeIPs(clientset *client.Clientset) ([]string, error) {
func getClusterNodeIPs(clientset client.Interface) ([]string, error) {
preferredAddressTypes := []api.NodeAddressType{
api.NodeExternalIP,
api.NodeLegacyHostIP,
}
nodeList, err := clientset.Nodes().List(metav1.ListOptions{})
nodeList, err := clientset.Core().Nodes().List(metav1.ListOptions{})
if err != nil {
return nil, err
}
Expand All @@ -461,7 +461,7 @@ func getClusterNodeIPs(clientset *client.Clientset) ([]string, error) {
return nodeAddresses, nil
}

func waitForLoadBalancerAddress(clientset *client.Clientset, svc *api.Service, dryRun bool) ([]string, []string, error) {
func waitForLoadBalancerAddress(clientset client.Interface, svc *api.Service, dryRun bool) ([]string, []string, error) {
ips := []string{}
hostnames := []string{}

Expand Down Expand Up @@ -521,7 +521,7 @@ func genCerts(svcNamespace, name, svcName, localDNSZoneName string, ips, hostnam
}, nil
}

func createAPIServerCredentialsSecret(clientset *client.Clientset, namespace, credentialsName string, entKeyPairs *entityKeyPairs, dryRun bool) (*api.Secret, error) {
func createAPIServerCredentialsSecret(clientset client.Interface, namespace, credentialsName string, entKeyPairs *entityKeyPairs, dryRun bool) (*api.Secret, error) {
// Build the secret object with API server credentials.
secret := &api.Secret{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -542,7 +542,7 @@ func createAPIServerCredentialsSecret(clientset *client.Clientset, namespace, cr
return clientset.Core().Secrets(namespace).Create(secret)
}

func createControllerManagerKubeconfigSecret(clientset *client.Clientset, namespace, name, svcName, kubeconfigName string, entKeyPairs *entityKeyPairs, dryRun bool) (*api.Secret, error) {
func createControllerManagerKubeconfigSecret(clientset client.Interface, namespace, name, svcName, kubeconfigName string, entKeyPairs *entityKeyPairs, dryRun bool) (*api.Secret, error) {
config := kubeconfigutil.CreateWithCerts(
fmt.Sprintf("https://%s", svcName),
name,
Expand All @@ -555,7 +555,7 @@ func createControllerManagerKubeconfigSecret(clientset *client.Clientset, namesp
return util.CreateKubeconfigSecret(clientset, config, namespace, kubeconfigName, dryRun)
}

func createPVC(clientset *client.Clientset, namespace, svcName, etcdPVCapacity string, dryRun bool) (*api.PersistentVolumeClaim, error) {
func createPVC(clientset client.Interface, namespace, svcName, etcdPVCapacity string, dryRun bool) (*api.PersistentVolumeClaim, error) {
capacity, err := resource.ParseQuantity(etcdPVCapacity)
if err != nil {
return nil, err
Expand Down Expand Up @@ -589,7 +589,7 @@ func createPVC(clientset *client.Clientset, namespace, svcName, etcdPVCapacity s
return clientset.Core().PersistentVolumeClaims(namespace).Create(pvc)
}

func createAPIServer(clientset *client.Clientset, namespace, name, image, credentialsName, advertiseAddress, storageBackend string, argOverrides map[string]string, pvc *api.PersistentVolumeClaim, dryRun bool) (*extensions.Deployment, error) {
func createAPIServer(clientset client.Interface, namespace, name, image, credentialsName, advertiseAddress, storageBackend string, argOverrides map[string]string, pvc *api.PersistentVolumeClaim, dryRun bool) (*extensions.Deployment, error) {
command := []string{
"/hyperkube",
"federation-apiserver",
Expand Down Expand Up @@ -704,7 +704,7 @@ func createAPIServer(clientset *client.Clientset, namespace, name, image, creden
return clientset.Extensions().Deployments(namespace).Create(dep)
}

func createControllerManagerSA(clientset *client.Clientset, namespace string, dryRun bool) (*api.ServiceAccount, error) {
func createControllerManagerSA(clientset client.Interface, namespace string, dryRun bool) (*api.ServiceAccount, error) {
sa := &api.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: ControllerManagerSA,
Expand All @@ -718,7 +718,7 @@ func createControllerManagerSA(clientset *client.Clientset, namespace string, dr
return clientset.Core().ServiceAccounts(namespace).Create(sa)
}

func createRoleBindings(clientset *client.Clientset, namespace, saName string, dryRun bool) (*rbac.Role, *rbac.RoleBinding, error) {
func createRoleBindings(clientset client.Interface, namespace, saName string, dryRun bool) (*rbac.Role, *rbac.RoleBinding, error) {
roleName := "federation-system:federation-controller-manager"
role := &rbac.Role{
// a role to use for bootstrapping the federation-controller-manager so it can access
Expand Down Expand Up @@ -752,7 +752,7 @@ func createRoleBindings(clientset *client.Clientset, namespace, saName string, d
return newRole, newRolebinding, err
}

func createControllerManager(clientset *client.Clientset, namespace, name, svcName, cmName, image, kubeconfigName, dnsZoneName, dnsProvider, saName string, dnsProviderSecret *api.Secret, argOverrides map[string]string, dryRun bool) (*extensions.Deployment, error) {
func createControllerManager(clientset client.Interface, namespace, name, svcName, cmName, image, kubeconfigName, dnsZoneName, dnsProvider, saName string, dnsProviderSecret *api.Secret, argOverrides map[string]string, dryRun bool) (*extensions.Deployment, error) {
command := []string{
"/hyperkube",
"federation-controller-manager",
Expand Down Expand Up @@ -879,7 +879,7 @@ func argMapsToArgStrings(argsMap, overrides map[string]string) []string {
return args
}

func waitForPods(clientset *client.Clientset, fedPods []string, namespace string) error {
func waitForPods(clientset client.Interface, fedPods []string, namespace string) error {
err := wait.PollInfinite(podWaitInterval, func() (bool, error) {
podCheck := len(fedPods)
podList, err := clientset.Core().Pods(namespace).List(metav1.ListOptions{})
Expand Down Expand Up @@ -979,7 +979,7 @@ func updateKubeconfig(config util.AdminConfig, name, endpoint, kubeConfigPath st
return nil
}

func createDNSProviderConfigSecret(clientset *client.Clientset, namespace, name string, dnsProviderConfigBytes []byte, dryRun bool) (*api.Secret, error) {
func createDNSProviderConfigSecret(clientset client.Interface, namespace, name string, dnsProviderConfigBytes []byte, dryRun bool) (*api.Secret, error) {
if dnsProviderConfigBytes == nil {
return nil, nil
}
Expand Down
6 changes: 3 additions & 3 deletions federation/pkg/kubefed/join.go
Expand Up @@ -213,7 +213,7 @@ func minifyConfig(clientConfig *clientcmdapi.Config, context string) (*clientcmd

// createSecret extracts the kubeconfig for a given cluster and populates
// a secret with that kubeconfig.
func createSecret(clientset *internalclientset.Clientset, clientConfig *clientcmdapi.Config, namespace, contextName, secretName string, dryRun bool) (runtime.Object, error) {
func createSecret(clientset internalclientset.Interface, clientConfig *clientcmdapi.Config, namespace, contextName, secretName string, dryRun bool) (runtime.Object, error) {
// Minify the kubeconfig to ensure that there is only information
// relevant to the cluster we are registering.
newClientConfig, err := minifyConfig(clientConfig, contextName)
Expand All @@ -236,7 +236,7 @@ func createSecret(clientset *internalclientset.Clientset, clientConfig *clientcm
// createConfigMap creates a configmap with name kube-dns in the joining cluster
// which stores the information about this federation zone name.
// If the configmap with this name already exists, its updated with this information.
func createConfigMap(hostClientSet *internalclientset.Clientset, config util.AdminConfig, fedSystemNamespace, targetClusterContext, kubeconfigPath string, dryRun bool) (*api.ConfigMap, error) {
func createConfigMap(hostClientSet internalclientset.Interface, config util.AdminConfig, fedSystemNamespace, targetClusterContext, kubeconfigPath string, dryRun bool) (*api.ConfigMap, error) {
cmDep, err := getCMDeployment(hostClientSet, fedSystemNamespace)
if err != nil {
return nil, err
Expand Down Expand Up @@ -337,7 +337,7 @@ func extractScheme(url string) string {
return scheme
}

func getCMDeployment(hostClientSet *internalclientset.Clientset, fedNamespace string) (*extensions.Deployment, error) {
func getCMDeployment(hostClientSet internalclientset.Interface, fedNamespace string) (*extensions.Deployment, error) {
depList, err := hostClientSet.Extensions().Deployments(fedNamespace).List(metav1.ListOptions{})
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions federation/pkg/kubefed/unjoin.go
Expand Up @@ -162,7 +162,7 @@ func popCluster(f cmdutil.Factory, name string) (*federationapi.Cluster, error)
return cluster, rh.Delete("", name)
}

func deleteConfigMapFromCluster(hostClientset *internalclientset.Clientset, secret *api.Secret, cluster *federationapi.Cluster, fedSystemNamespace string) error {
func deleteConfigMapFromCluster(hostClientset internalclientset.Interface, secret *api.Secret, cluster *federationapi.Cluster, fedSystemNamespace string) error {
clientset, err := getClientsetFromCluster(secret, cluster)
if err != nil {
return err
Expand Down Expand Up @@ -197,7 +197,7 @@ func deleteConfigMapFromCluster(hostClientset *internalclientset.Clientset, secr

// deleteSecret deletes the secret with the given name from the host
// cluster.
func deleteSecret(clientset *internalclientset.Clientset, name, namespace string) error {
func deleteSecret(clientset internalclientset.Interface, name, namespace string) error {
return clientset.Core().Secrets(namespace).Delete(name, &metav1.DeleteOptions{})
}

Expand Down
2 changes: 1 addition & 1 deletion federation/pkg/kubefed/util/util.go
Expand Up @@ -136,7 +136,7 @@ func (o *SubcommandOptions) SetName(cmd *cobra.Command, args []string) error {
return nil
}

func CreateKubeconfigSecret(clientset *client.Clientset, kubeconfig *clientcmdapi.Config, namespace, name string, dryRun bool) (*api.Secret, error) {
func CreateKubeconfigSecret(clientset client.Interface, kubeconfig *clientcmdapi.Config, namespace, name string, dryRun bool) (*api.Secret, error) {
configBytes, err := clientcmd.Write(*kubeconfig)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions pkg/kubectl/cmd/apply.go
Expand Up @@ -438,7 +438,7 @@ func getRESTMappings(mapper meta.RESTMapper, pruneResources *[]pruneResource) (n
type pruner struct {
mapper meta.RESTMapper
clientFunc resource.ClientMapperFunc
clientsetFunc func() (*internalclientset.Clientset, error)
clientsetFunc func() (internalclientset.Interface, error)

visitedUids sets.String
selector labels.Selector
Expand Down Expand Up @@ -500,7 +500,7 @@ func (p *pruner) delete(namespace, name string, mapping *meta.RESTMapping, c res
return runDelete(namespace, name, mapping, c, nil, p.cascade, p.gracePeriod, p.clientsetFunc)
}

func runDelete(namespace, name string, mapping *meta.RESTMapping, c resource.RESTClient, helper *resource.Helper, cascade bool, gracePeriod int, clientsetFunc func() (*internalclientset.Clientset, error)) error {
func runDelete(namespace, name string, mapping *meta.RESTMapping, c resource.RESTClient, helper *resource.Helper, cascade bool, gracePeriod int, clientsetFunc func() (internalclientset.Interface, error)) error {
if !cascade {
if helper == nil {
helper = resource.NewHelper(c, mapping)
Expand Down Expand Up @@ -538,7 +538,7 @@ type patcher struct {

mapping *meta.RESTMapping
helper *resource.Helper
clientsetFunc func() (*internalclientset.Clientset, error)
clientsetFunc func() (internalclientset.Interface, error)

overwrite bool
backOff clockwork.Clock
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubectl/cmd/drain.go
Expand Up @@ -47,7 +47,7 @@ import (
)

type DrainOptions struct {
client *internalclientset.Clientset
client internalclientset.Interface
restClient *restclient.RESTClient
factory cmdutil.Factory
Force bool
Expand Down Expand Up @@ -583,7 +583,7 @@ func (o *DrainOptions) waitForDelete(pods []api.Pod, interval, timeout time.Dura

// SupportEviction uses Discovery API to find out if the server support eviction subresource
// If support, it will return its groupVersion; Otherwise, it will return ""
func SupportEviction(clientset *internalclientset.Clientset) (string, error) {
func SupportEviction(clientset internalclientset.Interface) (string, error) {
discoveryClient := clientset.Discovery()
groupList, err := discoveryClient.ServerGroups()
if err != nil {
Expand Down
18 changes: 11 additions & 7 deletions pkg/kubectl/cmd/logs.go
Expand Up @@ -54,13 +54,19 @@ var (
kubectl logs --tail=20 nginx

# Show all logs from pod nginx written in the last hour
kubectl logs --since=1h nginx`)
kubectl logs --since=1h nginx

# Return snapshot logs from first container of a job named hello
kubectl logs job/hello

# Return snapshot logs from container nginx-1 of a deployment named nginx
kubectl logs deployment/nginx -c nginx-1`)

selectorTail int64 = 10
)

const (
logsUsageStr = "expected 'logs POD_NAME [CONTAINER_NAME]'.\nPOD_NAME is a required argument for the logs command"
logsUsageStr = "expected 'logs (POD | TYPE/NAME) [CONTAINER_NAME]'.\nPOD or TYPE/NAME is a required argument for the logs command"
)

type LogsOptions struct {
Expand All @@ -83,9 +89,9 @@ type LogsOptions struct {
func NewCmdLogs(f cmdutil.Factory, out io.Writer) *cobra.Command {
o := &LogsOptions{}
cmd := &cobra.Command{
Use: "logs [-f] [-p] POD [-c CONTAINER]",
Use: "logs [-f] [-p] (POD | TYPE/NAME) [-c CONTAINER]",
Short: i18n.T("Print the logs for a container in a pod"),
Long: "Print the logs for a container in a pod. If the pod has only one container, the container name is optional.",
Long: "Print the logs for a container in a pod or specified resource. If the pod has only one container, the container name is optional.",
Example: logs_example,
PreRun: func(cmd *cobra.Command, args []string) {
if len(os.Args) > 1 && os.Args[1] == "log" {
Expand All @@ -94,9 +100,7 @@ func NewCmdLogs(f cmdutil.Factory, out io.Writer) *cobra.Command {
},
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(o.Complete(f, out, cmd, args))
if err := o.Validate(); err != nil {
cmdutil.CheckErr(cmdutil.UsageError(cmd, err.Error()))
}
cmdutil.CheckErr(o.Validate())
Copy link
Contributor

@shiywang shiywang Feb 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove this ? if a user provided an invalid input, shouldn't we prompt this ?

unexpected logs options object 
See 'logs -h' for help and examples.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All user input is verified in Complete, at this point we're validating the correctness of internal structures (here LogsOptions) before running the actual command. User has no real input on these values, thus this doesn't provide any value for him. See other commands for similarity.

Copy link
Contributor

@shiywang shiywang Feb 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks

cmdutil.CheckErr(o.RunLogs())
},
Aliases: []string{"log"},
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubectl/cmd/rollingupdate.go
Expand Up @@ -245,7 +245,7 @@ func RunRollingUpdate(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args
// than the old rc. This selector is the hash of the rc, with a suffix to provide uniqueness for
// same-image updates.
if len(image) != 0 {
codec := api.Codecs.LegacyCodec(clientset.CoreClient.RESTClient().APIVersion())
codec := api.Codecs.LegacyCodec(v1.SchemeGroupVersion)
keepOldName = len(args) == 1
newName := findNewName(args, oldRc)
if newRc, err = kubectl.LoadExistingNextReplicationController(coreClient, cmdNamespace, newName); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/kubectl/cmd/testing/fake.go
Expand Up @@ -286,7 +286,7 @@ func (f *FakeFactory) RESTClient() (*restclient.RESTClient, error) {
return nil, nil
}

func (f *FakeFactory) ClientSet() (*internalclientset.Clientset, error) {
func (f *FakeFactory) ClientSet() (internalclientset.Interface, error) {
return nil, nil
}

Expand All @@ -311,7 +311,7 @@ func (f *FakeFactory) FederationClientSetForVersion(version *schema.GroupVersion
func (f *FakeFactory) FederationClientForVersion(version *schema.GroupVersion) (*restclient.RESTClient, error) {
return nil, nil
}
func (f *FakeFactory) ClientSetForVersion(requiredVersion *schema.GroupVersion) (*internalclientset.Clientset, error) {
func (f *FakeFactory) ClientSetForVersion(requiredVersion *schema.GroupVersion) (internalclientset.Interface, error) {
return nil, nil
}
func (f *FakeFactory) ClientConfigForVersion(requiredVersion *schema.GroupVersion) (*restclient.Config, error) {
Expand Down Expand Up @@ -536,7 +536,7 @@ func (f *fakeAPIFactory) JSONEncoder() runtime.Encoder {
return testapi.Default.Codec()
}

func (f *fakeAPIFactory) ClientSet() (*internalclientset.Clientset, error) {
func (f *fakeAPIFactory) ClientSet() (internalclientset.Interface, error) {
// Swap the HTTP client out of the REST client with the fake
// version.
fakeClient := f.tf.Client.(*fake.RESTClient)
Expand Down
5 changes: 5 additions & 0 deletions pkg/kubectl/cmd/util/BUILD
Expand Up @@ -75,6 +75,7 @@ go_test(
name = "go_default_test",
srcs = [
"cached_discovery_test.go",
"factory_object_mapping_test.go",
"factory_test.go",
"helpers_test.go",
"shortcut_restmapper_test.go",
Expand All @@ -90,7 +91,10 @@ go_test(
"//pkg/api/testing:go_default_library",
"//pkg/api/v1:go_default_library",
"//pkg/api/validation:go_default_library",
"//pkg/apis/apps:go_default_library",
"//pkg/apis/batch:go_default_library",
"//pkg/apis/extensions:go_default_library",
"//pkg/client/clientset_generated/internalclientset:go_default_library",
"//pkg/client/clientset_generated/internalclientset/fake:go_default_library",
"//pkg/controller:go_default_library",
"//pkg/kubectl:go_default_library",
Expand All @@ -104,6 +108,7 @@ go_test(
"//vendor:k8s.io/apimachinery/pkg/labels",
"//vendor:k8s.io/apimachinery/pkg/runtime",
"//vendor:k8s.io/apimachinery/pkg/runtime/schema",
"//vendor:k8s.io/apimachinery/pkg/util/diff",
"//vendor:k8s.io/apimachinery/pkg/util/validation/field",
"//vendor:k8s.io/apimachinery/pkg/version",
"//vendor:k8s.io/apimachinery/pkg/watch",
Expand Down
6 changes: 3 additions & 3 deletions pkg/kubectl/cmd/util/clientcache.go
Expand Up @@ -32,7 +32,7 @@ import (

func NewClientCache(loader clientcmd.ClientConfig, discoveryClientFactory DiscoveryClientFactory) *ClientCache {
return &ClientCache{
clientsets: make(map[schema.GroupVersion]*internalclientset.Clientset),
clientsets: make(map[schema.GroupVersion]internalclientset.Interface),
configs: make(map[schema.GroupVersion]*restclient.Config),
fedClientSets: make(map[schema.GroupVersion]fedclientset.Interface),
loader: loader,
Expand All @@ -44,7 +44,7 @@ func NewClientCache(loader clientcmd.ClientConfig, discoveryClientFactory Discov
// is invoked only once
type ClientCache struct {
loader clientcmd.ClientConfig
clientsets map[schema.GroupVersion]*internalclientset.Clientset
clientsets map[schema.GroupVersion]internalclientset.Interface
fedClientSets map[schema.GroupVersion]fedclientset.Interface
configs map[schema.GroupVersion]*restclient.Config

Expand Down Expand Up @@ -144,7 +144,7 @@ func copyConfig(in *restclient.Config) *restclient.Config {

// ClientSetForVersion initializes or reuses a clientset for the specified version, or returns an
// error if that is not possible
func (c *ClientCache) ClientSetForVersion(requiredVersion *schema.GroupVersion) (*internalclientset.Clientset, error) {
func (c *ClientCache) ClientSetForVersion(requiredVersion *schema.GroupVersion) (internalclientset.Interface, error) {
if requiredVersion != nil {
if clientset, ok := c.clientsets[*requiredVersion]; ok {
return clientset, nil
Expand Down