Skip to content

Commit

Permalink
Use docker to build cassandra jar file
Browse files Browse the repository at this point in the history
  • Loading branch information
aledbf committed Feb 25, 2018
1 parent 2845293 commit cabf8b8
Show file tree
Hide file tree
Showing 10 changed files with 325 additions and 266 deletions.
19 changes: 12 additions & 7 deletions cassandra/image/Makefile → cassandra/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,29 @@
# limitations under the License.

# build the cassandra image.
VERSION=v13
VERSION=v14
PROJECT_ID?=google_samples
PROJECT=gcr.io/${PROJECT_ID}
CASSANDRA_VERSION=3.11.2

all: kubernetes-cassandra.jar build

kubernetes-cassandra.jar: ../java/* ../java/src/main/java/io/k8s/cassandra/*.java
cd ../java && mvn clean && mvn package
mv ../java/target/kubernetes-cassandra*.jar files/kubernetes-cassandra.jar
cd ../java && mvn clean
build-go:
go build -a -installsuffix cgo \
-ldflags "-s -w" \
-o image/files/cassandra-seed.so -buildmode=c-shared go/main.go

kubernetes-cassandra.jar:
@echo "Building kubernetes-cassandra.jar"
docker run -v ${PWD}/java:/usr/src/app maven:3-jdk-8-onbuild-alpine mvn clean install
cp java/target/kubernetes-cassandra*.jar image/files/kubernetes-cassandra.jar

container:
@echo "Building ${PROJECT}/cassandra:${VERSION}"
docker build --pull --build-arg "CASSANDRA_VERSION=${CASSANDRA_VERSION}" -t ${PROJECT}/cassandra:${VERSION} .
docker build --pull --build-arg "CASSANDRA_VERSION=${CASSANDRA_VERSION}" -t ${PROJECT}/cassandra:${VERSION} image

container-dev:
docker build --pull --build-arg "CASSANDRA_VERSION=${CASSANDRA_VERSION}" --build-arg "DEV_CONTAINER=true" -t ${PROJECT}/cassandra:${VERSION}-dev .
docker build --pull --build-arg "CASSANDRA_VERSION=${CASSANDRA_VERSION}" --build-arg "DEV_CONTAINER=true" -t ${PROJECT}/cassandra:${VERSION}-dev image

build: container container-dev

Expand Down
4 changes: 3 additions & 1 deletion cassandra/cassandra-statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ spec:
terminationGracePeriodSeconds: 1800
containers:
- name: cassandra
image: gcr.io/google-samples/cassandra:v13
image: gcr.io/google-samples/cassandra:v14
imagePullPolicy: Always
ports:
- containerPort: 7000
Expand Down Expand Up @@ -60,6 +60,8 @@ spec:
value: "DC1-K8Demo"
- name: CASSANDRA_RACK
value: "Rack1-K8Demo"
- name: CASSANDRA_SEED_PROVIDER
value: io.k8s.cassandra.KubernetesSeedProvider
- name: POD_IP
valueFrom:
fieldRef:
Expand Down
82 changes: 82 additions & 0 deletions cassandra/go/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package main

// #include <stdio.h>
// #include <stdlib.h>
import "C"

import (
"context"
"encoding/json"
"log"
"strings"
"unicode"
"unsafe"

"github.com/ericchiang/k8s"
corev1 "github.com/ericchiang/k8s/apis/core/v1"
)

type endpoints struct {
IPs []string `json:"ips"`
}

// GetEndpoints searches the endpoints of a service returning a list of IP addresses.
//export GetEndpoints
func GetEndpoints(namespace, service, defSeeds *C.char) *C.char {
ns := C.GoString(namespace)
svc := C.GoString(service)
seeds := C.GoString(defSeeds)

s := strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return r
}, seeds)

nseeds := strings.Split(s, ",")
client, err := k8s.NewInClusterClient()
if err != nil {
log.Printf("unexpected error opening a connection against API server: %v\n", err)
log.Printf("returning default seeds: %v\n", nseeds)
return buildEndpoints(nseeds)
}

ips := make([]string, 0)

var endpoints corev1.Endpoints
err = client.Get(context.Background(), ns, svc, &endpoints)
if err != nil {
log.Printf("unexpected error obtaining information about service endpoints: %v\n", err)
log.Printf("returning default seeds: %v\n", nseeds)
return buildEndpoints(nseeds)
}

for _, endpoint := range endpoints.Subsets {
for _, address := range endpoint.Addresses {
ips = append(ips, *address.Ip)
}
}

if len(ips) == 0 {
return buildEndpoints(nseeds)
}

return buildEndpoints(ips)
}

func buildEndpoints(ips []string) *C.char {
b, err := json.Marshal(&endpoints{ips})
if err != nil {
log.Printf("unexpected error serializing JSON response: %v\n", err)
rc := C.CString(`{"ips":[]}`)
defer C.free(unsafe.Pointer(rc))
return rc
}

rc := C.CString(string(b))
defer C.free(unsafe.Pointer(rc))
return rc
}

func main() {}
4 changes: 4 additions & 0 deletions cassandra/image/files/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ else
rm -rf $CASSANDRA_HOME/pylib;
fi

mv /kubernetes-cassandra.jar /usr/local/apache-cassandra-${CASSANDRA_VERSION}/lib
mv /cassandra-seed.so /etc/cassandra/
mv /cassandra-seed.h /usr/local/lib/include

apt-get -y purge localepurge
apt-get -y autoremove
apt-get clean
Expand Down
79 changes: 79 additions & 0 deletions cassandra/image/files/cassandra-seed.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* Created by "go tool cgo" - DO NOT EDIT. */

/* package command-line-arguments */


#line 1 "cgo-builtin-prolog"

#include <stddef.h> /* for ptrdiff_t below */

#ifndef GO_CGO_EXPORT_PROLOGUE_H
#define GO_CGO_EXPORT_PROLOGUE_H

typedef struct { const char *p; ptrdiff_t n; } _GoString_;

#endif

/* Start of preamble from import "C" comments. */


#line 3 "/home/aledbf/go/src/k8s.io/examples/cassandra/go/main.go"
#include <stdio.h>
#include <stdlib.h>

#line 1 "cgo-generated-wrapper"


/* End of preamble from import "C" comments. */


/* Start of boilerplate cgo prologue. */
#line 1 "cgo-gcc-export-header-prolog"

#ifndef GO_CGO_PROLOGUE_H
#define GO_CGO_PROLOGUE_H

typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef unsigned short GoUint16;
typedef int GoInt32;
typedef unsigned int GoUint32;
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt64 GoInt;
typedef GoUint64 GoUint;
typedef __SIZE_TYPE__ GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;

/*
static assertion to make sure the file is being used on architecture
at least with matching size of GoInt.
*/
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];

typedef _GoString_ GoString;
typedef void *GoMap;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;

#endif

/* End of boilerplate cgo prologue. */

#ifdef __cplusplus
extern "C" {
#endif


// GetEndpoints searches the endpoints of a service returning a list of IP addresses.

extern char* GetEndpoints(char* p0, char* p1, char* p2);

#ifdef __cplusplus
}
#endif
Binary file modified cassandra/image/files/kubernetes-cassandra.jar
Binary file not shown.
12 changes: 5 additions & 7 deletions cassandra/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.k8s.cassandra</groupId>
<artifactId>kubernetes-cassandra</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
Expand All @@ -33,7 +33,7 @@

<properties>
<logback.version>1.1.3</logback.version>
<cassandra.version>3.9</cassandra.version>
<cassandra.version>3.11.2</cassandra.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -61,7 +61,6 @@
<version>${logback.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
Expand All @@ -72,14 +71,13 @@
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.6.3</version>
<version>1.9.13</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.6.3</version>
<version>1.9.13</version>
<scope>provided</scope>
</dependency>

Expand Down
54 changes: 54 additions & 0 deletions cassandra/java/src/main/java/io/k8s/cassandra/GoInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

/*
* Copyright (C) 2018 Google 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 io.k8s.cassandra;

import java.util.Arrays;
import java.util.List;

import com.sun.jna.Library;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;

public interface GoInterface extends Library {
public String GetEndpoints(String namespace, String service, String seeds);

public class GoSlice extends Structure {
public static class ByValue extends GoSlice implements Structure.ByValue {
}

public Pointer data;
public long len;
public long cap;

protected List<String> getFieldOrder() {
return Arrays.asList(new String[] { "data", "len", "cap" });
}
}

public class GoString extends Structure {
public static class ByValue extends GoString implements Structure.ByValue {
}

public String p;
public long n;

protected List<String> getFieldOrder() {
return Arrays.asList(new String[] { "p", "n" });
}
}
}
Loading

0 comments on commit cabf8b8

Please sign in to comment.