Skip to content
This repository has been archived by the owner on Oct 24, 2018. It is now read-only.

Commit

Permalink
@#31 | add k8s resource
Browse files Browse the repository at this point in the history
  • Loading branch information
hieptranquoc committed Feb 23, 2018
1 parent 4ec478d commit 8203203
Show file tree
Hide file tree
Showing 21 changed files with 646 additions and 37 deletions.
32 changes: 25 additions & 7 deletions .kitchen.dokken.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
driver:
name: dokken
chef_version: latest
privileged: true

transport:
name: dokken
Expand All @@ -15,15 +16,32 @@ verifier:
platforms:
- name: ubuntu-16.04
driver:
image: ubuntu:16.04
# pid_one_command: /bin/systemd
image: dokken/ubuntu-16.04
privileged: true
pid_one_command: /bin/systemd
intermediate_instructions:
- RUN /usr/bin/apt-get update
- RUN /usr/bin/apt-get -y install curl lsb-release bash-completion
- RUN /usr/bin/apt-get -y install curl lsb-release bash-completion dbus
- RUN echo "SSHD_OPTS='-o UseDNS=no -o UsePAM=no -o PasswordAuthentication=yes -o UsePrivilegeSeparation=no -o PidFile=/tmp/sshd.pid'" > /etc/default/ssh
- RUN ln -s /lib/systemd/system/systemd-logind.service /etc/systemd/system/multi-user.target.wants/systemd-logind.service
- RUN mkdir -p /etc/systemd/system/sockets.target.wants/
- RUN ln -s /lib/systemd/system/dbus.socket /etc/systemd/system/sockets.target.wants/dbus.socket
- RUN systemctl set-default multi-user.target
- name: centos-7
driver:
image: centos:7
privileged: true
# pid_one_command: /usr/lib/systemd/systemd
image: dokken/centos-7
platform: rhel
pid_one_command: /usr/lib/systemd/systemd
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro # required by systemd
intermediate_instructions:
- RUN yum -y install lsof which net-tools curl bash-completion
- RUN yum -y update
- RUN yum -y install lsof which systemd-sysv initscripts openssl net-tools curl bash-completion dbus
- RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done)
- RUN rm -f /lib/systemd/system/multi-user.target.wants/*
- RUN rm -f /etc/systemd/system/*.wants/*
- RUN rm -f /lib/systemd/system/local-fs.target.wants/*
- RUN rm -f /lib/systemd/system/sockets.target.wants/*udev*
- RUN rm -f /lib/systemd/system/sockets.target.wants/*initctl*
- RUN rm -f /lib/systemd/system/basic.target.wants/*
- RUN rm -f /lib/systemd/system/anaconda.target.wants/*
27 changes: 20 additions & 7 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ verifier:

platforms:
- name: ubuntu-16.04
- name: centos-7.3
- name: centos-7

suites:
- name: smoke-install-default
Expand All @@ -23,20 +23,24 @@ suites:
verifier:
inspec_tests:
- test/smoke/default/default_test.rb
- test/smoke/gcloud/gcloud_install_default_test.rb
- test/smoke/kubectl/kubectl_install_default_test.rb
- test/smoke/gcloud/gcloud_install_default_test.rb
- test/smoke/helm/helm_install_default_test.rb
- test/smoke/kubernetes/kubernetes_install_default_test.rb
attributes:
kubernetes-stack:
kubectl:
enabled: true,
version: ""
version: "v1.8.0"
gcloud:
enabled: true,
version: ""
helm:
enabled: true,
version: ""
kubernetes:
enabled: true,
version: ""

- name: smoke-install-specific-version
run_list:
Expand All @@ -45,33 +49,42 @@ suites:
inspec_tests:
- test/smoke/default/default_test.rb
- test/smoke/gcloud/gcloud_install_specific_version_test.rb
- test/smoke/kubectl/kubectl_install_specific_version_test.rb
- test/smoke/helm/helm_install_specific_version_test.rb
- test/smoke/kubectl/kubectl_install_specific_version_test.rb
- test/smoke/kubernetes/kubernetes_install_specific_version_test.rb
attributes:
kubernetes-stack:
kubectl:
enabled: true,
version: "v1.7.0"
version: "v1.8.0"
gcloud:
enabled: true,
version: "158.0.0"
version: "164.0.0"
helm:
enabled: true,
version: "v2.4.2"
kubernetes:
enabled: true,
version: "v1.8.0"

- name: smoke-uninstall
run_list:
- recipe[kubernetes-stack-test::uninstall]
verifier:
inspec_tests:
- test/smoke/default/default_test.rb
- test/smoke/gcloud/gcloud_uninstall_test.rb
- test/smoke/kubectl/kubectl_uninstall_test.rb
- test/smoke/helm/helm_uninstall_test.rb
- test/smoke/kubectl/kubectl_uninstall_test.rb
- test/smoke/kubernetes/kubernetes_uninstall_test.rb
attributes:
kubernetes-stack:
kubectl:
enabled: true,
version: ""
kubernetes:
enabled: true,
version: ""
gcloud:
enabled: true,
version: ""
Expand Down
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ env:
- INSTANCE=smoke-uninstall-ubuntu-1604
- INSTANCE=smoke-uninstall-centos-7

before_install:
# install the latest docker and docker-compose versions
- sudo apt-get remove docker docker-engine
- sudo curl -sSL https://get.docker.com/ | sh
- docker version
# install
install: true
# Ensure we make ChefDK's Ruby the default
before_script:
- sudo iptables -L DOCKER || ( echo "DOCKER iptables chain missing" ; sudo iptables -N DOCKER )
Expand Down
49 changes: 47 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Kubernetes stack cookbook to work with Kubernetes: https://supermarket.chef.io/c
- `kubectl`: support all centos-7 and ubuntu-16.04 versions.
- `helm`: support all centos-7 and ubuntu-16.04 versions.
- `gcloud`: support all centos-7 and ubuntu-16.04 versions. Should use version avaiable in https://packages.cloud.google.com/apt/ (with ubuntu platform) for faster autocomplete.
- `kubernetes`: support all centos-7 and ubuntu-16.04 versions.

## How to use

Expand All @@ -26,7 +27,7 @@ Kubernetes stack cookbook to work with Kubernetes: https://supermarket.chef.io/c
kubectl 'install kubectl' do
action [:install, :remove]
version '' #application version (if empty, default: latest)
binary_path '' #application path (if empty, default: /usr/local/bin/kubectl)
binary_path '' #application path (if empty, default: /usr/local/bin)
end

gcloud 'install gcloud' do
Expand All @@ -40,6 +41,12 @@ helm 'install helm' do
version '' #application version (if empty, default: latest)
binary_path '' #application path (if empty, default: /usr/local/bin/helm)
end

kubernetes 'install kubernetes' do
action [:install, :remove]
version '' #application version (if empty, default: latest, ensure same as kubectl version)
binary_path '' #application path (if empty, default: /usr/local/bin)
end
```

## How to develop
Expand Down Expand Up @@ -85,6 +92,7 @@ end
- [gcloud](#gcloud): install or remove `google-cloud-sdk`.
- [kubectl](#kubectl): install or remove `kubectl`.
- [helm](#helm): install or remove `helm`.
- [kubernetes](#kubernetes): install or remove `kubernetes`.

## Resources detail
## gcloud
Expand Down Expand Up @@ -164,7 +172,7 @@ end
### Properties
- `action` - `:install` to install `kubectl`, `:remove` to uninstall `kubectl`.
- `version` - The desired version of `kubectl`.
- `binary_path` - Application path (if empty, default:/usr/local/bin/kubectl)
- `binary_path` - Application path (if empty, default:/usr/local/bin)

## helm

Expand Down Expand Up @@ -205,6 +213,43 @@ end
- `version` - The desired version of `helm`.
- `binary_path` - Application path (if empty, default:/usr/local/bin/helm)

## kubernetes

The `kubernetes` resource auto-install or auto-remove `kubernetes` with the provider resolution system.

### Example

Install `kubernetes` with default version and default solution:

```ruby
kubernetes 'install default kubernetes' do
action :install
version ''
end
```

Install `kubernetes` with specific version or specific solution:

```ruby
kubernetes 'install specific kubernetes version' do
action :install
version 'v1.9.0' # ensure same as kubectl version
end
```

Remove `kubernetes`:

```ruby
kubernetes 'remove kubernetes' do
action :remove
end
```

### Properties
- `action` - `:install` to install `kubernetes`, `:remove` to uninstall `kubernetes`.
- `version` - The desired version of `kubernetes`.
- `binary_path` - Application path (if empty, default:/usr/local/bin)

## See more:

- https://github.com/teracyhq/dev
Expand Down
4 changes: 4 additions & 0 deletions libraries/matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ def install_helm(message)
def install_gcloud(message)
ChefSpec::Matchers::ResourceMatcher.new(:gcloud, :install, message)
end

def install_kubernetes(message)
ChefSpec::Matchers::ResourceMatcher.new(:kubernetes, :install, message)
end
end
2 changes: 1 addition & 1 deletion resources/gcloud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@

# Gcloud version will be installed via file downloaded
else
download_url = "https://storage.googleapis.com/cloud-sdk-release/google-cloud-sdk-#{version}-linux-#{arch} -m`.tar.gz"
download_url = "https://storage.googleapis.com/cloud-sdk-release/google-cloud-sdk-#{version}-linux-#{arch}.tar.gz"

execute "curl #{download_url} | tar xvz" do
cwd install_path
Expand Down
24 changes: 17 additions & 7 deletions resources/kubectl.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
resource_name :kubectl

property :version, String, default: ''
property :binary_path, String, default: '/usr/local/bin/kubectl'
property :binary_path, String, default: '/usr/local/bin'

default_action :install

Expand Down Expand Up @@ -56,12 +56,21 @@
only_if 'which kubectl'
end

download_url = "https://storage.googleapis.com/kubernetes-release/release/#{version}/bin/linux/#{arch}/kubectl"
# download_url = "https://storage.googleapis.com/kubernetes-release/release/#{version}/bin/linux/#{arch}/kubectl"

remote_file binary_path do
source download_url
mode '0755'
not_if { ::File.exist?(binary_path) }
# remote_file binary_path do
# source download_url
# mode '0755'
# not_if { ::File.exist?(binary_path) }
# end

bash 'install kubectl' do
cwd binary_path
code <<-EOH
curl -LO https://storage.googleapis.com/kubernetes-release/release/#{version}/bin/linux/#{arch}/kubectl
chmod +x kubectl
EOH
not_if 'which kubectl'
end

package 'bash-completion'
Expand All @@ -80,13 +89,14 @@
command 'kubectl completion bash > /etc/bash_completion.d/kubectl'
creates '/etc/bash_completion.d/kubect'
user 'root'
only_if 'which kubectl'
end
end
end

action :remove do
execute 'remove kubectl' do
command "rm -rf #{binary_path}"
command "rm -rf #{binary_path}/kubectl"
only_if 'which kubectl'
end
end
Loading

0 comments on commit 8203203

Please sign in to comment.