github.com/danielqsj/helm@v2.0.0-alpha.4.0.20160908204436-976e0ba5199b+incompatible/pkg/helm/client.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     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 helm // import "k8s.io/helm/pkg/helm"
    18  
    19  import (
    20  	"os"
    21  
    22  	"google.golang.org/grpc"
    23  
    24  	"k8s.io/helm/pkg/chartutil"
    25  	rls "k8s.io/helm/pkg/proto/hapi/services"
    26  )
    27  
    28  const (
    29  	// HelmHostEnvVar is the $HELM_HOST envvar
    30  	HelmHostEnvVar = "HELM_HOST"
    31  
    32  	// HelmHomeEnvVar is the $HELM_HOME envvar
    33  	HelmHomeEnvVar = "HELM_HOME"
    34  
    35  	// DefaultHelmHost is the default tiller server host address.
    36  	DefaultHelmHost = ":44134"
    37  
    38  	// DefaultHelmHome is the default $HELM_HOME envvar value
    39  	DefaultHelmHome = "$HOME/.helm"
    40  )
    41  
    42  // Client manages client side of the helm-tiller protocol
    43  type Client struct {
    44  	opts options
    45  }
    46  
    47  // NewClient creates a new client.
    48  func NewClient(opts ...Option) *Client {
    49  	return new(Client).Init().Option(opts...)
    50  }
    51  
    52  // Option configures the helm client with the provided options
    53  func (h *Client) Option(opts ...Option) *Client {
    54  	for _, opt := range opts {
    55  		opt(&h.opts)
    56  	}
    57  	return h
    58  }
    59  
    60  // Init initializes the helm client with default options
    61  func (h *Client) Init() *Client {
    62  	return h.Option(Host(DefaultHelmHost)).
    63  		Option(Home(os.ExpandEnv(DefaultHelmHome)))
    64  }
    65  
    66  // ListReleases lists the current releases.
    67  func (h *Client) ListReleases(opts ...ReleaseListOption) (*rls.ListReleasesResponse, error) {
    68  	c, err := grpc.Dial(h.opts.host, grpc.WithInsecure())
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  	defer c.Close()
    73  
    74  	return h.opts.rpcListReleases(rls.NewReleaseServiceClient(c), opts...)
    75  }
    76  
    77  // InstallRelease installs a new chart and returns the release response.
    78  func (h *Client) InstallRelease(chStr, ns string, opts ...InstallOption) (*rls.InstallReleaseResponse, error) {
    79  	c, err := grpc.Dial(h.opts.host, grpc.WithInsecure())
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  	defer c.Close()
    84  
    85  	chart, err := chartutil.Load(chStr)
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  
    90  	return h.opts.rpcInstallRelease(chart, rls.NewReleaseServiceClient(c), ns, opts...)
    91  }
    92  
    93  // DeleteRelease uninstalls a named release and returns the response.
    94  //
    95  // Note: there aren't currently any supported DeleteOptions, but they are
    96  // kept in the API signature as a placeholder for future additions.
    97  func (h *Client) DeleteRelease(rlsName string, opts ...DeleteOption) (*rls.UninstallReleaseResponse, error) {
    98  	c, err := grpc.Dial(h.opts.host, grpc.WithInsecure())
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  	defer c.Close()
   103  
   104  	return h.opts.rpcDeleteRelease(rlsName, rls.NewReleaseServiceClient(c), opts...)
   105  }
   106  
   107  // UpdateRelease updates a release to a new/different chart
   108  func (h *Client) UpdateRelease(rlsName string, chStr string, opts ...UpdateOption) (*rls.UpdateReleaseResponse, error) {
   109  	c, err := grpc.Dial(h.opts.host, grpc.WithInsecure())
   110  	if err != nil {
   111  		return nil, err
   112  	}
   113  	defer c.Close()
   114  
   115  	chart, err := chartutil.Load(chStr)
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  
   120  	return h.opts.rpcUpdateRelease(rlsName, chart, rls.NewReleaseServiceClient(c), opts...)
   121  }
   122  
   123  // ReleaseStatus returns the given release's status.
   124  //
   125  // Note: there aren't currently any  supported StatusOptions,
   126  // but they are kept in the API signature as a placeholder for future additions.
   127  func (h *Client) ReleaseStatus(rlsName string, opts ...StatusOption) (*rls.GetReleaseStatusResponse, error) {
   128  	c, err := grpc.Dial(h.opts.host, grpc.WithInsecure())
   129  	if err != nil {
   130  		return nil, err
   131  	}
   132  	defer c.Close()
   133  
   134  	return h.opts.rpcGetReleaseStatus(rlsName, rls.NewReleaseServiceClient(c), opts...)
   135  }
   136  
   137  // ReleaseContent returns the configuration for a given release.
   138  //
   139  // Note: there aren't currently any supported ContentOptions, but
   140  // they are kept in the API signature as a placeholder for future additions.
   141  func (h *Client) ReleaseContent(rlsName string, opts ...ContentOption) (*rls.GetReleaseContentResponse, error) {
   142  	c, err := grpc.Dial(h.opts.host, grpc.WithInsecure())
   143  	if err != nil {
   144  		return nil, err
   145  	}
   146  	defer c.Close()
   147  
   148  	return h.opts.rpcGetReleaseContent(rlsName, rls.NewReleaseServiceClient(c), opts...)
   149  }