s2ibinary_handler.go 5.9 KB
Newer Older
S
shaowenchen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
Copyright 2020 The KubeSphere Authors.

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.
*/

R
runzexia 已提交
17 18 19 20 21
package devops

import (
	"code.cloudfoundry.org/bytefmt"
	"fmt"
R
runzexia 已提交
22 23
	"github.com/aws/aws-sdk-go/aws/awserr"
	awsS3 "github.com/aws/aws-sdk-go/service/s3"
R
runzexia 已提交
24 25 26
	"github.com/emicklei/go-restful"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/util/retry"
R
runzexia 已提交
27
	"k8s.io/klog"
R
runzexia 已提交
28
	"kubesphere.io/kubesphere/pkg/apis/devops/v1alpha1"
Z
zryfish 已提交
29 30 31
	"kubesphere.io/kubesphere/pkg/client/clientset/versioned"
	"kubesphere.io/kubesphere/pkg/client/informers/externalversions"
	"kubesphere.io/kubesphere/pkg/simple/client/s3"
R
runzexia 已提交
32 33 34 35 36 37
	"mime/multipart"
	"net/http"
	"reflect"
)

const (
R
runzexia 已提交
38
	GetS2iBinaryURL = "http://ks-apiserver.kubesphere-system.svc/kapis/devops.kubesphere.io/v1alpha2/namespaces/%s/s2ibinaries/%s/file/%s"
R
runzexia 已提交
39 40
)

Z
zryfish 已提交
41
type S2iBinaryUploader interface {
R
runzexia 已提交
42
	UploadS2iBinary(namespace, name, md5 string, header *multipart.FileHeader) (*v1alpha1.S2iBinary, error)
Z
zryfish 已提交
43

R
runzexia 已提交
44
	DownloadS2iBinary(namespace, name, fileName string) (string, error)
Z
zryfish 已提交
45
}
46

Z
zryfish 已提交
47 48 49 50 51 52
type s2iBinaryUploader struct {
	client    versioned.Interface
	informers externalversions.SharedInformerFactory
	s3Client  s3.Interface
}

R
runzexia 已提交
53 54 55 56 57 58 59 60
func NewS2iBinaryUploader(client versioned.Interface, informers externalversions.SharedInformerFactory, s3Client s3.Interface) S2iBinaryUploader {
	return &s2iBinaryUploader{
		client:    client,
		informers: informers,
		s3Client:  s3Client,
	}
}

Z
zryfish 已提交
61
func (s *s2iBinaryUploader) UploadS2iBinary(namespace, name, md5 string, fileHeader *multipart.FileHeader) (*v1alpha1.S2iBinary, error) {
R
runzexia 已提交
62 63
	binFile, err := fileHeader.Open()
	if err != nil {
R
runzexia 已提交
64
		klog.Errorf("%+v", err)
R
runzexia 已提交
65 66 67 68
		return nil, err
	}
	defer binFile.Close()

Z
zryfish 已提交
69
	origin, err := s.informers.Devops().V1alpha1().S2iBinaries().Lister().S2iBinaries(namespace).Get(name)
R
runzexia 已提交
70
	if err != nil {
R
runzexia 已提交
71
		klog.Errorf("%+v", err)
R
runzexia 已提交
72 73 74 75 76
		return nil, err
	}
	//Check file is uploading
	if origin.Status.Phase == v1alpha1.StatusUploading {
		err := restful.NewError(http.StatusConflict, "file is uploading, please try later")
R
runzexia 已提交
77
		klog.Error(err)
R
runzexia 已提交
78 79 80 81 82 83 84 85 86 87 88 89
		return nil, err
	}
	copy := origin.DeepCopy()
	copy.Spec.MD5 = md5
	copy.Spec.Size = bytefmt.ByteSize(uint64(fileHeader.Size))
	copy.Spec.FileName = fileHeader.Filename
	copy.Spec.DownloadURL = fmt.Sprintf(GetS2iBinaryURL, namespace, name, copy.Spec.FileName)
	if origin.Status.Phase == v1alpha1.StatusReady && reflect.DeepEqual(origin, copy) {
		return origin, nil
	}

	//Set status Uploading to lock resource
Z
zryfish 已提交
90
	uploading, err := s.SetS2iBinaryStatus(copy, v1alpha1.StatusUploading)
R
runzexia 已提交
91 92
	if err != nil {
		err := restful.NewError(http.StatusConflict, fmt.Sprintf("could not set status: %+v", err))
R
runzexia 已提交
93
		klog.Error(err)
R
runzexia 已提交
94 95
		return nil, err
	}
R
runzexia 已提交
96 97

	copy = uploading.DeepCopy()
R
runzexia 已提交
98 99 100 101
	copy.Spec.MD5 = md5
	copy.Spec.Size = bytefmt.ByteSize(uint64(fileHeader.Size))
	copy.Spec.FileName = fileHeader.Filename
	copy.Spec.DownloadURL = fmt.Sprintf(GetS2iBinaryURL, namespace, name, copy.Spec.FileName)
102

R
runzexia 已提交
103 104 105 106 107 108 109 110 111 112
	err = s.s3Client.Upload(fmt.Sprintf("%s-%s", namespace, name), copy.Spec.FileName, binFile)
	if err != nil {
		if aerr, ok := err.(awserr.Error); ok {
			switch aerr.Code() {
			case awsS3.ErrCodeNoSuchBucket:
				klog.Error(err)
				_, serr := s.SetS2iBinaryStatusWithRetry(copy, origin.Status.Phase)
				if serr != nil {
					klog.Error(serr)
				}
Z
zryfish 已提交
113
				return nil, err
R
runzexia 已提交
114 115 116 117 118
			default:
				klog.Error(err)
				_, serr := s.SetS2iBinaryStatusWithRetry(copy, v1alpha1.StatusUploadFailed)
				if serr != nil {
					klog.Error(serr)
R
runzexia 已提交
119
				}
R
runzexia 已提交
120
				return nil, err
R
runzexia 已提交
121 122
			}
		}
R
runzexia 已提交
123 124 125 126
		klog.Error(err)
		return nil, err
	}

R
runzexia 已提交
127 128 129 130
	if copy.Spec.UploadTimeStamp == nil {
		copy.Spec.UploadTimeStamp = new(metav1.Time)
	}
	*copy.Spec.UploadTimeStamp = metav1.Now()
R
runzexia 已提交
131
	copy, err = s.client.DevopsV1alpha1().S2iBinaries(namespace).Update(copy)
R
runzexia 已提交
132
	if err != nil {
R
runzexia 已提交
133
		klog.Error(err)
R
runzexia 已提交
134 135 136
		return nil, err
	}

Z
zryfish 已提交
137
	copy, err = s.SetS2iBinaryStatusWithRetry(copy, v1alpha1.StatusReady)
R
runzexia 已提交
138
	if err != nil {
R
runzexia 已提交
139
		klog.Error(err)
R
runzexia 已提交
140 141
		return nil, err
	}
R
runzexia 已提交
142
	return copy, nil
R
runzexia 已提交
143 144
}

Z
zryfish 已提交
145
func (s *s2iBinaryUploader) DownloadS2iBinary(namespace, name, fileName string) (string, error) {
146

Z
zryfish 已提交
147
	origin, err := s.informers.Devops().V1alpha1().S2iBinaries().Lister().S2iBinaries(namespace).Get(name)
R
runzexia 已提交
148
	if err != nil {
R
runzexia 已提交
149
		klog.Errorf("%+v", err)
R
runzexia 已提交
150 151
		return "", err
	}
Z
zryfish 已提交
152

R
runzexia 已提交
153 154
	if origin.Spec.FileName != fileName {
		err := fmt.Errorf("could not fould file %s", fileName)
R
runzexia 已提交
155
		klog.Error(err)
R
runzexia 已提交
156 157
		return "", err
	}
Z
zryfish 已提交
158

R
runzexia 已提交
159 160
	if origin.Status.Phase != v1alpha1.StatusReady {
		err := restful.NewError(http.StatusBadRequest, "file is not ready, please try later")
R
runzexia 已提交
161
		klog.Error(err)
R
runzexia 已提交
162 163
		return "", err
	}
R
runzexia 已提交
164
	return s.s3Client.GetDownloadURL(fmt.Sprintf("%s-%s", namespace, name), fileName)
R
runzexia 已提交
165 166
}

Z
zryfish 已提交
167
func (s *s2iBinaryUploader) SetS2iBinaryStatus(s2ibin *v1alpha1.S2iBinary, status string) (*v1alpha1.S2iBinary, error) {
R
runzexia 已提交
168 169
	copy := s2ibin.DeepCopy()
	copy.Status.Phase = status
Z
zryfish 已提交
170
	copy, err := s.client.DevopsV1alpha1().S2iBinaries(s2ibin.Namespace).Update(copy)
R
runzexia 已提交
171
	if err != nil {
R
runzexia 已提交
172
		klog.Error(err)
R
runzexia 已提交
173 174 175 176 177
		return nil, err
	}
	return copy, nil
}

Z
zryfish 已提交
178
func (s *s2iBinaryUploader) SetS2iBinaryStatusWithRetry(s2ibin *v1alpha1.S2iBinary, status string) (*v1alpha1.S2iBinary, error) {
R
runzexia 已提交
179 180 181 182

	var bin *v1alpha1.S2iBinary
	var err error
	err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
R
runzexia 已提交
183
		bin, err = s.client.DevopsV1alpha1().S2iBinaries(s2ibin.Namespace).Get(s2ibin.Name, metav1.GetOptions{})
R
runzexia 已提交
184
		if err != nil {
R
runzexia 已提交
185
			klog.Error(err)
R
runzexia 已提交
186 187 188
			return err
		}
		bin.Status.Phase = status
Z
zryfish 已提交
189
		bin, err = s.client.DevopsV1alpha1().S2iBinaries(s2ibin.Namespace).Update(bin)
R
runzexia 已提交
190
		if err != nil {
R
runzexia 已提交
191
			klog.Error(err)
R
runzexia 已提交
192 193 194 195 196
			return err
		}
		return nil
	})
	if err != nil {
R
runzexia 已提交
197
		klog.Error(err)
R
runzexia 已提交
198 199 200 201 202
		return nil, err
	}

	return bin, nil
}